<?php

class wms_ctl_admin_receipts_print extends desktop_controller
{

    var $workground = "wms_delivery";

    var $dlyCorp_tab = 'show';



    public function getShopIds(){

        if (!$_POST['shop_id']) {
            $shop_id = $this->shopIds;
        } else {
            $shop_id= $_POST['shop_id'];
        }
        return $shop_id;
    }




    /**
     * 添加发货/单据打印下的顶部view
     */
    function _views()
    {
        if ($this->dlyCorp_tab == 'hidden') {
            return '';
        }

        $status = kernel::single('base_component_request')->get_get('status');
        $sku = kernel::single('base_component_request')->get_get('sku');

        $query = array(
            'app' => 'wms',
            'ctl' => 'admin_receipts_print',
            'act' => 'index',
            'status' => $status,
            'sku' => $sku,
        );
        //所有自建仓
        $ownerBranch = array();
        $ownerBranch = kernel::single('ome_branch_type')->getOwnBranchIds();

        $sub_menu = $this->getView($status);
        $i = 0;

        $mdl_order = app::get('wms')->model('delivery');
        $start=time();
        foreach ($sub_menu as $k => $v) {
            //非管理员取管辖仓与自建仓的交集
            $v['filter']['ext_branch_id'] = $v['filter']['ext_branch_id'] ? array_intersect($v['filter']['ext_branch_id'], $ownerBranch) : $ownerBranch;
            $sub_menu[$k]['filter'] = $v['filter'] ? $v['filter'] : null;
            $sub_menu[$k]['addon'] = $mdl_order->viewcount($v['filter']);
            
            $query['view'] = $i++;
            $query['logi_id'] = urlencode($v['filter']['logi_id']);
            $sub_menu[$k]['href'] = 'index.php?' . http_build_query($query);
            unset($v);
        }
        $end=time();
        error_log('开始'.$start.'-'.$end.PHP_EOL, 3, DATA_DIR.'/logs/toprinttime'.date('Y-m-d').'.log');
        return $sub_menu;
    }

    function getView($status)
    {
        $oDlycorp = app::get('ome')->model('dly_corp');
        $submenu = $oDlycorp->getList('corp_id,name', array('disabled' => 'false'));

        if (empty($submenu))
            return $submenu;

        $tmp_filter = array('type' => 'normal');
        $s_filter = $this->analyseStatus($status);
        $tmp_filter = array_merge($tmp_filter, $s_filter);

        $oBranch = app::get('ome')->model('branch');
        $is_super = kernel::single('desktop_user')->is_super();
        if (!$is_super) {
            $branch_ids = $oBranch->getBranchByUser(true);
            if ($branch_ids) {
                $tmp_filter['ext_branch_id'] = $_POST['branch_id'] ? $_POST['branch_id'] : $branch_ids;
            } else {
                $tmp_filter['ext_branch_id'] = 'false';
            }
        }

        $c = 1;
        $sub_menu[0] = array(
            'label' => $tmp_filter['_title_'],
            'filter' => $tmp_filter,
            'optional' => false
        );

        #第三方发货时，显示已发货、未发货
        if ($_GET['ctl'] == 'admin_receipts_outer') {
            $c = 3;#物流公司view在未发货之后
            $outer_filter = $tmp_filter;
            #让已发货、未发货显示在全部之后
            $outer = $this->shipStatus();
            foreach ($outer as $key => $v) {
                if ($key == 'succ') {
                    $outer_filter['status'] = array(3);
                } else {
                    #所有不属于发货成功的，都是未发货
                    $outer_filter['status'] = array(0);
                    //unset($tmp_filter['status']);
                }
                $sub_menu[] = array(
                    'label' => app::get('base')->_($v),
                    'filter' => $outer_filter,
                    'optional' => false
                );
            }
        }

        foreach ($submenu as $keys => $values) {
            $sub_menu[$c] = array(
                'label' => $values['name'],
                'filter' => array_merge($tmp_filter, array('logi_id' => $values['corp_id'])),
                'optional' => false
            );
            $c++;
        }
        return $sub_menu;
    }

    public function notice()
    {
        $this->page('admin/receipts/notice.html');
    }

    function index()
    {
        if ($_GET['status'] == '' || $_GET['status'] == 5) {
            $this->dlyCorp_tab = 'hidden';
        }
        # 操作员ID号
        $op_id = $this->user->get_id();
        $sku = kernel::single('base_component_request')->get_get('sku');

        $cfgr = app::get('wms')->getConf('wms.delivery.cfg.radio');

        if (empty($cfgr) && $_GET['status'] == 0) $cfgr = 1;
        if ($cfgr == 2 && isset($sku) && $sku == '') {

            $jumpto = app::get('wms')->base_url(1) . 'index.php?app=wms&ctl=admin_receipts_print&act=index&status=0&sku=single';
            $this->pagedata['msg'] = '对不起！由于您设置了按品类打印，请去单品，多品打印！将单品和多品分开打印有助于提升效率！';
            $this->pagedata['jumpto'] = $jumpto;
            $this->pagedata['wait'] = 3;
            $this->display('splash/notice.html', 'desktop');
            exit;
        }

        if ($cfgr == 1 && isset($sku) && in_array($sku, array('single', 'multi'))) {
            $jumpto = app::get('wms')->base_url(1) . 'index.php?app=wms&ctl=admin_receipts_print&act=index&status=0&sku=';
            $this->pagedata['msg'] = '对不起！由于您设置了经典打印，请去待打印打印！';
            $this->pagedata['jumpto'] = $jumpto;
            $this->pagedata['wait'] = 3;
            $this->display('splash/notice.html', 'desktop');
            exit;
        }

        # 发货配置
        $deliCfgLib = kernel::single('wms_delivery_cfg');

        $title = '';

        $filter['type'] = 'normal';
        if (!$_POST['shop_id']) {
            $filter['shop_id'] = $this->shopIds;
        } else {
            $filter['shop_id'] = $_POST['shop_id'];
        }

        //分析status的filter条件
        $tmp_filter = $this->analyseStatus($_GET['status']);
        $filter = array_merge($filter, $tmp_filter);

        //获取操作员管辖仓库
        if ($_GET['btype'] == 2) {
            //导出的时候走这里取第三方仓
            $ownerBranch = array();
            $ownerBranch = kernel::single('ome_branch_type')->getOtherBranchLists();
        } else {
            //所有自建仓
            $ownerBranch = array();
            $ownerBranch = kernel::single('ome_branch_type')->getOwnBranchIds();
        }

        # 获取操作员管辖仓库
        $is_super = kernel::single('desktop_user')->is_super();
        if (!$is_super) {
            $oBranch = app::get('ome')->model('branch');
            $branch_ids = $oBranch->getBranchByUser(true);
            if ($branch_ids) {
                $filter['ext_branch_id'] = $_POST['branch_id'] ? $_POST['branch_id'] : $branch_ids;

                $filter['ext_branch_id'] = array_intersect($filter['ext_branch_id'], $ownerBranch);
            } else {
                $filter['ext_branch_id'] = 'false';
            }
        } else {
            $filter['ext_branch_id'] = $ownerBranch;
        }
        $attach = '&status=' . $_GET['status'] . '&logi_id=' . $_GET['logi_id'];
        if (isset($sku)) $attach .= '&sku=' . $sku;
        $use_buildin_import = false;
        $user = kernel::single('desktop_user');

        if ($user->has_permission('wms_process_receipts_print_export')) {
            $use_buildin_import = true;
        }
        //add by ldward 20160831 未发货则不允许显示在待打印中
        if ($_GET['status'] == 0 || $_GET['status'] == 2 || $_GET['status'] == 4) {
            //array_push($filter, $value1['shipdocno']);
            $filter['is_ship'] = 'true';
            $filter['is_form'] = 'true';
            $filter['deliverytype|noequal'] = 'pickup';//自提不能显示在待打印中

        }
        //add by ldward 20180523 过滤区域
        if($_POST['area'])
        {
            $filter['ship_province']=$_POST['area'];
        }

        if(base_request::get_remote_addr()=='101.229.86.227')
        {
            error_log(var_export(array($filter),true),3,PUBLIC_DIR."/lakers11-2.log");
        }

        $url = base_request::get_request_uri() . '&action=export';
        $params = array(
            'title' => $filter['_title_'],
            'actions' => array(
                'stock' => array(
                    'label' => '打印备货单',
                    'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=toPrintStock' . $attach,
                    'target' => "_blank",
                ),
                'delie' => array(
                    'label' => '打印发货单',
                    'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=toPrintMerge' . $attach,
                    'target' => '_blank',
                ),
                'merge' => array(
                    'label' => '联合打印',
                    'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=toMergePrint' . $attach,
                    'target' => '_blank',
                ),
                'expre' => array(
                    'label' => '打印快递单',
                    'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=toPrintShip' . $attach,
                    'target' => '_blank',
                ),
            ),
            'base_filter' => $filter,
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag' => false,
            'use_buildin_recycle' => false,
            'use_buildin_export' => $use_buildin_import,
            'use_buildin_import' => false,
            'use_buildin_filter' => true,
            'max_actions' => 8,
            'use_view_tab' => true,
            'finder_aliasname' => 'delivery_print' . $op_id,
            //从载方法 以解决 发货中未录入快递单号不能过滤的bug
            'object_method' => array('count' => 'count_logi_no', 'getlist' => 'getlist_logi_no'),
        );


        //发货模板配置
        $delivery_cfg = app::get('wms')->getConf('wms.delivery.status.cfg');

        //发货单控件风格
        if (isset($delivery_cfg['set']['wms_delivery_print_mode']) && $delivery_cfg['set']['wms_delivery_print_mode'] == 1) {
            $params['actions']['delie']['submit'] = 'index.php?app=wms&ctl=admin_receipts_print&act=toPrintMergeNew' . $attach;
        }

        //备货控件风格
        if (isset($delivery_cfg['set']['wms_stock_print_mode']) && $delivery_cfg['set']['wms_stock_print_mode'] == 1) {
            $params['actions']['stock']['submit'] = 'index.php?app=wms&ctl=admin_receipts_print&act=toPrintStockNew' . $attach;
        }
        $rolesData = app::get('desktop')->model('hasrole')->getList('role_id', array('user_id' => $op_id));
        foreach ($rolesData as $key => $value) {
            if ($value['role_id'] == 7) {
                $is_printship = '1';
            }
        }
        if (app::get('logisticsmanager')->is_installed()) 
        {

            unset($params['actions']['expre']);
            $params['actions']['newexpre'] = array(
                'label' => '打印快递单',
                'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=toPrintExpre' . $attach,
                'target' => '_blank',
            );
            
            if (!$user->has_permission('wms_ctl_admin_toPrintShip')) 
            {
                if($_GET['status']!=0)
                {
                    unset($params['actions']['newexpre']);
                }
            }

            //打印快递单按钮显示,临时使用
            if (($_GET['status'] == 1 || $_GET['status'] == 5 || $_GET['status'] == '') && $is_printship && ($op_id !=='49' || $op_id !=='127' || $op_id !=='128')) {
               // unset($params['actions']['newexpre']);
            }
            //打印快递单按钮显示,临时使用
            if(($_GET['status'] == 1 ||  $_GET['status'] == 4) && $is_super && ($op_id !=='49' || $op_id !=='127' || $op_id !=='128')) {
                unset($params['actions']['newexpre']);
            }
        }

        //判断未打印的列表页可以设置列表分页到500
        if ($_GET['status'] == 0) {
            //$this->max_plimit = 500;
            //$params['plimit_in_sel'] = array(500, 400, 300, 200, 100, 50, 20, 10);

            if (app::get('ome')->getConf('delivery.bycreatetime' . $op_id) == 1) {
                $label = '按默认排序显示';
                $order_val = 0;
            } else {
                $label = '按成单时间显示';
                $order_val = 1;
            }
            $params['actions']['orderbycreatetime'] = array('label' => $label, 'href' => 'index.php?app=wms&ctl=admin_receipts_print&act=orderbycreatetime&p[0]=' . (isset($sku) ? $sku : 'page_all') . '&p[1]=' . $order_val . '&p[2]=' . $op_id);
        }

        //选择显示打印的按钮
        $showStockBtn = $deliCfgLib->analyse_btn_status('stock', $sku);
        if ($showStockBtn == false) {
            unset($params['actions']['stock']);
        }
        $showDelieBtn = $deliCfgLib->analyse_btn_status('delie', $sku);
        if ($showDelieBtn == false) {
            unset($params['actions']['delie']);
        }
        $showMergeBtn = $deliCfgLib->analyse_btn_status('merge', $sku);
        if ($showMergeBtn == false) {
            unset($params['actions']['merge']);
        }

        //暂停列表不显示按钮
        if ($_GET['status'] == 6) {
            unset($params['actions']['stock']);
            unset($params['actions']['delie']);
            unset($params['actions']['style']);
            unset($params['actions']['expre']);
            unset($params['actions']['merge']);
        }

        # 如果是第一个TAB 弹出对话框
        if ($_GET['view'] == 0 || empty($_GET['view'])) {
            foreach ($params['actions'] as $key => $act) {
                $act['confirm'] = "我们强烈建议打印任务都在进入各物流公司的分页夹后进行，在全部里只进行查找等操作及显示结果的处理，以避免出现不该发生的错误。你还确定要进行打印操作吗？\n\n注意：操作前请确认打印机中的面单和要打印的单据相匹配。";
                $params['actions'][$key] = $act;
            }
        }

        # 在列表上方添加搜索
        if (!isset($_GET['action'])) {
            $panel = new desktop_panel($this);
            $panel->setId('delivery_finder_top');
            $panel->setTmpl('admin/finder/finder_panel_filter.html');
            $panel->show('wms_mdl_delivery', $params);
        }

        # 多打印模板--独立按钮
        if ($_GET['status'] != 6) {
            $otmplModel = app::get('ome')->model('print_otmpl');
            $filter = array('disabled' => 'false', 'aloneBtn' => 'true', 'open' => 'true', 'type' => array('delivery', 'stock'));
            $aloneBtnList = $otmplModel->getList('id,btnName,type', $filter);

            $typeAct = array('delivery' => 'toPrintMerge', 'stock' => 'toPrintStock');
            foreach ($aloneBtnList as $key => $value) {
                $params['actions']['aloneBtn' . $key] = array(
                    'label' => $value['btnName'],
                    'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=' . $typeAct[$value['type']] . $attach . '&otmplId=' . $value['id'],
                    'target' => '_blank',
                );
            }
            //独立按钮
            $expressObj = app::get('logisticsmanager')->model('express_template');
            $alone_filter = array('aloneBtn' => 'true', 'status' => 'true', 'template_type' => array('delivery', 'stock'));
            $alonexpressList = $expressObj->getList('template_id,btnName,template_type', $alone_filter);

            $typeelectl = array('delivery' => 'toPrintMergeNew', 'stock' => 'toPrintStockNew');
            foreach ($alonexpressList as $k => $v) {
                $params['actions']['aloneBtn' . $k] = array(
                    'label' => $v['btnName'],
                    'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=' . $typeelectl[$v['template_type']] . $attach . '&otmplId=' . $v['template_id'],
                    'target' => '_blank',
                );
            }
        }

        # 批量更换物流按钮
        $istoChangeDly = kernel::single('desktop_user')->has_permission('wms_process_receipts_print_toChangeDly');
            
        if ($istoChangeDly) 
        {
            if ($_GET['status'] == 0) 
            {
                $params['actions']['changeDly'] = array(
                    'label' => '批量更换物流',
                    'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=toChangeDly' . $attach,
                    'target' => 'dialog::{title:\'批量更换物流公司\',width:680,height:500}',
                );
            }
        }
        $params['actions']['batch_oms'] = array(
            'label' => '等待配货',
            'href' => 'index.php?app=wms&ctl=admin_receipts_print&act=batch_oms',
        );
        if ($_GET['status'] == '5') {
            $params['actions']['batchsync'] = array('label' => '发货状态回写至OMS',
                'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=batch_sync',
                'confirm' => '你确定要对勾选的发货单状态回写吗？',
                'target' => 'refresh');
        }
        $params['actions']['sendAxCustom'] = array(
            'label' => '发送AX快递费',
            'href' => 'index.php?app=wms&ctl=admin_receipts_print&act=sendAxCustom' . $attach,
            'target' => 'dialog::{title:\'发送AX快递费\',width:680,height:500}',
        );
         $isgoback = kernel::single('desktop_user')->has_permission('delivery_deliveryReCalc');
            
        if ($isgoback) 
        {
            $params['actions']['ReCalc'] = array(
                'label' => '重算快递费',
                'href' => 'index.php?app=wms&ctl=admin_receipts_print&act=ReCalc' . $attach,         
                'target' => 'dialog::{title:\'重算快递费\',width:700,height:500}',
            );
        }
        $name = $this->user->get_login_name();
        if ($name == 'MonsterXZ') {
            $params['actions']['updateDelivery'] = array(
                'label' => '修改运费',
                'href' => 'index.php?app=wms&ctl=admin_lwdelivery&act=updateDelivery' . $attach,
                'target' => 'dialog::{title:\'修改运费\',width:400,height:300}',
            );
            $params['actions']['updateOrder'] = array(
                'label' => '修改订单金额',
                'href' => 'index.php?app=wms&ctl=admin_lwdelivery&act=updateOrder' . $attach,
                'target' => 'dialog::{title:\'修改订单金额\',width:400,height:300}',
            );
        }
        //var_dump($params['base_filter']);
        $this->finder('wms_mdl_delivery', $params);
    }

  function outerorilistindex()
    {
        if ($_GET['status'] == '' || $_GET['status'] == 7) {
            $this->dlyCorp_tab = 'hidden';
        }
        # 操作员ID号
        $op_id = $this->user->get_id();
        $sku = kernel::single('base_component_request')->get_get('sku');

        $cfgr = app::get('wms')->getConf('wms.delivery.cfg.radio');

        if (empty($cfgr) && $_GET['status'] == 0) $cfgr = 1;
        if ($cfgr == 2 && isset($sku) && $sku == '') {

            $jumpto = app::get('wms')->base_url(1) . 'index.php?app=wms&ctl=admin_receipts_print&act=index&status=0&sku=single';
            $this->pagedata['msg'] = '对不起！由于您设置了按品类打印，请去单品，多品打印！将单品和多品分开打印有助于提升效率！';
            $this->pagedata['jumpto'] = $jumpto;
            $this->pagedata['wait'] = 3;
            $this->display('splash/notice.html', 'desktop');
            exit;
        }

        if ($cfgr == 1 && isset($sku) && in_array($sku, array('single', 'multi'))) {
            $jumpto = app::get('wms')->base_url(1) . 'index.php?app=wms&ctl=admin_receipts_print&act=index&status=0&sku=';
            $this->pagedata['msg'] = '对不起！由于您设置了经典打印，请去待打印打印！';
            $this->pagedata['jumpto'] = $jumpto;
            $this->pagedata['wait'] = 3;
            $this->display('splash/notice.html', 'desktop');
            exit;
        }

        # 发货配置
        $deliCfgLib = kernel::single('wms_delivery_cfg');

        $title = '';

        $filter['type'] = 'normal';
        if (!$_POST['shop_id']) {
            $filter['shop_id'] = $this->shopIds;
        } else {
            $filter['shop_id'] = $_POST['shop_id'];
        }

        //分析status的filter条件
        $tmp_filter = $this->analyseStatus($_GET['status']);
        $filter = array_merge($filter, $tmp_filter);
        
        //获取操作员管辖仓库
         $oBranch = app::get('ome')->model('branch');
        //所有第三方仓
        $outerBranch = array();
        $tmpBranchList = $oBranch->getList('branch_id',array('owner'=>'2'));
        foreach ($tmpBranchList as $key => $value) {
            $outerBranch[] = $value['branch_id'];
        }
        unset($tmpBranchList);
         # 发货配置
        $deliCfgLib = kernel::single('wms_delivery_cfg');
        //获取操作员管辖仓库
        $is_super = kernel::single('desktop_user')->is_super();
        if (!$is_super) {
            $branch_ids = $oBranch->getBranchByUser(true);
            if ($branch_ids) {
                $filter['ext_branch_id'] = $_POST['branch_id'] ? $_POST['branch_id'] : $branch_ids;

                $filter['ext_branch_id'] = array_intersect($filter['ext_branch_id'], $outerBranch);
            } else {
                $filter['ext_branch_id'] = 'false';
            }
        } else {
            $filter['ext_branch_id'] = $outerBranch;
        }
        $attach = '&status=' . $_GET['status'] . '&logi_id=' . $_GET['logi_id'];
        if (isset($sku)) $attach .= '&sku=' . $sku;
        $use_buildin_import = false;
        $user = kernel::single('desktop_user');

        if ($user->has_permission('wms_process_receipts_print_export')) {
            $use_buildin_import = true;
        }
        
        //add by ldward 20160831 未发货则不允许显示在待打印中
        if ($_GET['status'] == 0 || $_GET['status'] == 2 || $_GET['status'] == 4) {
            //array_push($filter, $value1['shipdocno']);
            $filter['is_ship'] = 'true';
            $filter['is_form'] = 'true';
            $filter['deliverytype|noequal'] = 'pickup';//自提不能显示在待打印中

        }
        if(base_request::get_remote_addr()=='101.229.86.227')
        {
            error_log(var_export(array($filter),true),3,PUBLIC_DIR."/lakers11-2.log");
        }

        $url = base_request::get_request_uri() . '&action=export';
        $params = array(
            'title' => $filter['_title_'],
            'actions' => array(
                'stock' => array(
                    'label' => '打印备货单',
                    'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=toPrintStock' . $attach,
                    'target' => "_blank",
                ),
                'delie' => array(
                    'label' => '打印发货单',
                    'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=toPrintMerge' . $attach,
                    'target' => '_blank',
                ),
                'merge' => array(
                    'label' => '联合打印',
                    'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=toMergePrint' . $attach,
                    'target' => '_blank',
                ),
                'expre' => array(
                    'label' => '打印快递单',
                    'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=toPrintShip' . $attach,
                    'target' => '_blank',
                ),
            ),
            'base_filter' => $filter,
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag' => false,
            'use_buildin_recycle' => false,
            'use_buildin_export' => $use_buildin_import,
            'use_buildin_import' => false,
            'use_buildin_filter' => true,
            'max_actions' => 8,
            'use_view_tab' => true,
            'finder_aliasname' => 'delivery_print' . $op_id,
            //从载方法 以解决 发货中未录入快递单号不能过滤的bug
            'object_method' => array('count' => 'count_logi_no', 'getlist' => 'getlist_logi_no'),
        );


        //发货模板配置
        $delivery_cfg = app::get('wms')->getConf('wms.delivery.status.cfg');

        //发货单控件风格
        if (isset($delivery_cfg['set']['wms_delivery_print_mode']) && $delivery_cfg['set']['wms_delivery_print_mode'] == 1) {
            $params['actions']['delie']['submit'] = 'index.php?app=wms&ctl=admin_receipts_print&act=toPrintMergeNew' . $attach;
        }

        //备货控件风格
        if (isset($delivery_cfg['set']['wms_stock_print_mode']) && $delivery_cfg['set']['wms_stock_print_mode'] == 1) {
            $params['actions']['stock']['submit'] = 'index.php?app=wms&ctl=admin_receipts_print&act=toPrintStockNew' . $attach;
        }
        $rolesData = app::get('desktop')->model('hasrole')->getList('role_id', array('user_id' => $op_id));
        foreach ($rolesData as $key => $value) {
            if ($value['role_id'] == 7) {
                $is_printship = '1';
            }
        }
        if (app::get('logisticsmanager')->is_installed()) {
            $params['actions']['newexpre'] = array(
                'label' => '打印快递单',
                'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=toPrintExpre' . $attach,
                'target' => '_blank',
            );

            //打印快递单按钮显示,临时使用
            if (($_GET['status'] == 1 || $_GET['status'] == 5 || $_GET['status'] == '') && $is_printship && ($op_id !=='49' || $op_id !=='127' || $op_id !=='128')) {
               // unset($params['actions']['newexpre']);
            }
            //打印快递单按钮显示,临时使用
            if(($_GET['status'] == 1 ||  $_GET['status'] == 4) && $is_super && ($op_id !=='49' || $op_id !=='127' || $op_id !=='128')) {
                unset($params['actions']['newexpre']);
            }

            unset($params['actions']['expre']);
        }

        //判断未打印的列表页可以设置列表分页到500
        if ($_GET['status'] == 0) {
            //$this->max_plimit = 500;
            //$params['plimit_in_sel'] = array(500, 400, 300, 200, 100, 50, 20, 10);

            if (app::get('ome')->getConf('delivery.bycreatetime' . $op_id) == 1) {
                $label = '按默认排序显示';
                $order_val = 0;
            } else {
                $label = '按成单时间显示';
                $order_val = 1;
            }
            $params['actions']['orderbycreatetime'] = array('label' => $label, 'href' => 'index.php?app=wms&ctl=admin_receipts_print&act=orderbycreatetime&p[0]=' . (isset($sku) ? $sku : 'page_all') . '&p[1]=' . $order_val . '&p[2]=' . $op_id);
        }

        //选择显示打印的按钮
        $showStockBtn = $deliCfgLib->analyse_btn_status('stock', $sku);
        if ($showStockBtn == false) {
            unset($params['actions']['stock']);
        }
        $showDelieBtn = $deliCfgLib->analyse_btn_status('delie', $sku);
        if ($showDelieBtn == false) {
            unset($params['actions']['delie']);
        }
        $showMergeBtn = $deliCfgLib->analyse_btn_status('merge', $sku);
        if ($showMergeBtn == false) {
            unset($params['actions']['merge']);
        }

        //暂停列表不显示按钮
        if ($_GET['status'] == 6) {
            unset($params['actions']['stock']);
            unset($params['actions']['delie']);
            unset($params['actions']['style']);
            unset($params['actions']['expre']);
            unset($params['actions']['merge']);
        }

        # 如果是第一个TAB 弹出对话框
        if ($_GET['view'] == 0 || empty($_GET['view'])) {
            foreach ($params['actions'] as $key => $act) {
                $act['confirm'] = "我们强烈建议打印任务都在进入各物流公司的分页夹后进行，在全部里只进行查找等操作及显示结果的处理，以避免出现不该发生的错误。你还确定要进行打印操作吗？\n\n注意：操作前请确认打印机中的面单和要打印的单据相匹配。";
                $params['actions'][$key] = $act;
            }
        }

        # 在列表上方添加搜索
        if (!isset($_GET['action'])) {
            $panel = new desktop_panel($this);
            $panel->setId('delivery_finder_top');
            $panel->setTmpl('admin/finder/finder_panel_filter.html');
            $panel->show('wms_mdl_delivery', $params);
        }

        # 多打印模板--独立按钮
        if ($_GET['status'] != 6) {
            $otmplModel = app::get('ome')->model('print_otmpl');
            $filter = array('disabled' => 'false', 'aloneBtn' => 'true', 'open' => 'true', 'type' => array('delivery', 'stock'));
            $aloneBtnList = $otmplModel->getList('id,btnName,type', $filter);

            $typeAct = array('delivery' => 'toPrintMerge', 'stock' => 'toPrintStock');
            foreach ($aloneBtnList as $key => $value) {
                $params['actions']['aloneBtn' . $key] = array(
                    'label' => $value['btnName'],
                    'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=' . $typeAct[$value['type']] . $attach . '&otmplId=' . $value['id'],
                    'target' => '_blank',
                );
            }
            //独立按钮
            $expressObj = app::get('logisticsmanager')->model('express_template');
            $alone_filter = array('aloneBtn' => 'true', 'status' => 'true', 'template_type' => array('delivery', 'stock'));
            $alonexpressList = $expressObj->getList('template_id,btnName,template_type', $alone_filter);

            $typeelectl = array('delivery' => 'toPrintMergeNew', 'stock' => 'toPrintStockNew');
            foreach ($alonexpressList as $k => $v) {
                $params['actions']['aloneBtn' . $k] = array(
                    'label' => $v['btnName'],
                    'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=' . $typeelectl[$v['template_type']] . $attach . '&otmplId=' . $v['template_id'],
                    'target' => '_blank',
                );
            }
        }

        # 批量更换物流按钮
        if($_GET['status']!=7)
        {
             $istoChangeDly = kernel::single('desktop_user')->has_permission('wms_process_receipts_print_toChangeDly');
            
            if ($istoChangeDly) 
            { 
                if ($_GET['status'] == 0) 
                {
                    $params['actions']['changeDly'] = array(
                        'label' => '批量更换物流',
                        'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=toChangeDly' . $attach,
                        'target' => 'dialog::{title:\'批量更换物流公司\',width:680,height:500}',
                    );
                }
            }
            $params['actions']['batch_oms'] = array(
                'label' => '等待配货',
                'href' => 'index.php?app=wms&ctl=admin_receipts_print&act=batch_oms',
            );
            if ($_GET['status'] == '5') {
                $params['actions']['batchsync'] = array('label' => '发货状态回写至OMS',
                    'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=batch_sync',
                    'confirm' => '你确定要对勾选的发货单状态回写吗？',
                    'target' => 'refresh');
            }
            $params['actions']['sendAxCustom'] = array(
                'label' => '发送AX快递费',
                'href' => 'index.php?app=wms&ctl=admin_receipts_print&act=sendAxCustom' . $attach,
                'target' => 'dialog::{title:\'发送AX快递费\',width:680,height:500}',
            );     
            $isgoback = kernel::single('desktop_user')->has_permission('delivery_deliveryReCalc');
            
            if ($isgoback) 
            {
                $params['actions']['ReCalc'] = array(
                    'label' => '重算快递费',
                    'href' => 'index.php?app=wms&ctl=admin_receipts_print&act=ReCalc' . $attach,
                    'target' => 'dialog::{title:\'重算快递费\',width:700,height:500}',
                );
            }
        }
        $name = $this->user->get_login_name();
        if ($name == 'MonsterXZ') {
            $params['actions']['updateDelivery'] = array(
                'label' => '修改运费',
                'href' => 'index.php?app=wms&ctl=admin_lwdelivery&act=updateDelivery' . $attach,
                'target' => 'dialog::{title:\'修改运费\',width:400,height:300}',
            );
            $params['actions']['updateOrder'] = array(
                'label' => '修改订单金额',
                'href' => 'index.php?app=wms&ctl=admin_lwdelivery&act=updateOrder' . $attach,
                'target' => 'dialog::{title:\'修改订单金额\',width:400,height:300}',
            );
        }
        //add by ldward 20180409 添加自提判断过滤
        $params['base_filter']['deliverytype'] = array('pickup','delivery','auto','batch','single');
      
        $this->finder('wms_mdl_delivery', $params);
    }

 


    //add by ldward 20160824 新增菜单
    function customerindex()
    {

        //分析status的filter条件
        $tmp_filter = $this->analyseStatus($_GET['status']);
        $filter = array_merge($filter, $tmp_filter);
        $filter['print_status'] = '0';
        $filter['status'] = '0';
        $filter['is_ship'] = 'false';
        $filter['deliverytype'] = 'delivery';
        if (!$_POST['shop_id']) {
            $filter['shop_id'] = $this->shopIds;
        } else {
            $filter['shop_id'] = $_POST['shop_id'];
        }
        $params = array(
            'title' => '待发货列表',
            'actions' => array(
                'stock' => array(
                    'label' => '备货',
                    'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=DoAxso&status=1' . $attach,
                ),
            ),
            'base_filter' => $filter,
            //'base_filter' => '',
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag' => false,
            'use_buildin_recycle' => false,
            'use_buildin_export' => false,
            'use_buildin_import' => false,
            'use_buildin_filter' => true,
            'max_actions' => 8,
            'use_view_tab' => false,
            'finder_aliasname' => 'delivery_print' . $op_id,
            //从载方法 以解决 发货中未录入快递单号不能过滤的bug
            'object_method' => array('count' => 'count_logi_no', 'getlist' => 'getlist_logi_no'),
        );
        $this->finder('wms_mdl_delivery', $params);
    }

    //add by ldward20160930 门店自提业务
    function customerseftindex()
    {

        //分析status的filter条件
        $tmp_filter = $this->analyseStatus($_GET['status']);
        $filter = array_merge($filter, $tmp_filter);
        $filter['print_status'] = '0';
        $filter['status'] = '0';
        $filter['is_ship'] = 'true';
        $filter['deliverytype'] = 'pickup';

        if (!$_POST['shop_id']) {
            $filter['shop_id'] = $this->shopIds;
        } else {
            $filter['shop_id'] = $_POST['shop_id'];
        }
        $params = array(
            'title' => '门店自提',
            'actions' => array(
                'stock' => array(
                    'label' => '确认发货',
//                    'submit' => 'index.php?app=wms&ctl=admin_receipts_consign&act=consign&status=1' . $attach,
                    'submit' => 'index.php?app=wms&ctl=admin_receipts_print&act=doSelfAxso&status=1' . $attach,
                ),
            ),
            'base_filter' => $filter,
            //'base_filter' => '',
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag' => false,
            'use_buildin_recycle' => false,
            'use_buildin_export' => false,
            'use_buildin_import' => false,
            'use_buildin_filter' => true,
            'max_actions' => 8,
            'use_view_tab' => false,
            'finder_aliasname' => 'delivery_print' . $op_id,
            //从载方法 以解决 发货中未录入快递单号不能过滤的bug
            'object_method' => array('count' => 'count_logi_no', 'getlist' => 'getlist_logi_no'),
        );
        $this->finder('wms_mdl_delivery', $params);
    }

    function doSelfAxso()
    {
        $max_input_vars = ini_get('max_input_vars');
        ini_set('memory_limit', '512M');
        set_time_limit(600);
        $this->begin("index.php?app=wms&ctl=admin_receipts_print&act=customerseftindex&status=1");
        $orders_obj = $this->app->model('delivery');
        $this->_request = kernel::single('base_component_request');
        $post_info = $this->_request->get_post();
        $post_info['print_status'] = '0';
        $post_info['status'] = '0';
        $post_info['is_ship'] = 'false';
        if ($post_info['isSelectedAll'] == '_ALL_') {
            $order_ids = $orders_obj->getList('delivery_id', $post_info, 0, -1);
            for ($i = 0; $i < sizeof($order_ids); $i++) {
                $post_info['delivery_id'][] = $order_ids[$i]['delivery_id'];
            }
        } else {
            $input = file_get_contents("php://input");
            $input_array = explode('&', $input);
            $number = count($input_array);
            if ($number > $max_input_vars) {
                $msg = "多选不能超过系统表单最大提交数【" . $max_input_vars . "】条";
                $this->end(false, $msg);
            }
        }
        if (empty($post_info['delivery_id'])) {
            $this->end(false, '无发货信息选中');
        }
        $arr_order_id = implode(',', $post_info['delivery_id']);
        $orders_obj->db->exec("update sdb_wms_delivery set status=3,print_status = 4,process_status=7,delivery_time=" . time() . " where  delivery_id in(" . $arr_order_id . ")");
        $this->end(true, '发货成功');
    }

    //add by ldward 生成AX订单及备货通知
    function DoAxso()
    {

        $this->begin("index.php?app=wms&ctl=admin_receipts_print&act=customerindex&status=1");

        $orders_obj = $this->app->model('delivery');

        $this->_request = kernel::single('base_component_request');
        $order_info = $this->_request->get_post();
        $order_info['print_status'] = '0';
        $order_info['status'] = '0';
        $order_info['is_ship'] = 'false';
        if ($order_info['isSelectedAll'] == '_ALL_') {
            $order_ids = $orders_obj->getList('*', $order_info, 0, -1);
            for ($i = 0; $i < sizeof($order_ids); $i++) {
                $order_info['delivery_id'][] = $order_ids[$i]['delivery_id'];
            }
        }
        if (empty($order_info['delivery_id'])) {
            $this->end(false, '无备货信息选中');
        }
//$order_info['delivery_id'][]=218 ; //测试
        $nowdate = date('Ymd', time()) . '01';
        $shop_obj = app::get('ome')->model('shop');
        $sales_material = app::get('material')->model('basic_material');
        $sales_material_ext = app::get('material')->model('basic_material_ext');
        $oms_order_obj = app::get('ome')->model('orders');
        $ax = array();
        //var_dump($order_info);
        kernel::log(json_encode($order_info));
        kernel::log(count($order_info['delivery_id']));
        foreach ($order_info['delivery_id'] as $key => $arr_order_id) {

            $orders = $orders_obj->getList('*', array('delivery_id' => $arr_order_id), 0, 1);
            //
            if ($orders[0]['stockno'] == '') {
                $shop_bn = $shop_obj->getRow($orders[0]['shop_id']);
                $shop_bn = $shop_bn['AXCustomer'];
                $ordersitems_obj = $this->app->model('delivery_items');
                $ordersbill_obj = $this->app->model('delivery_bill');
                $orders_items = $ordersitems_obj->getList('*', array('delivery_id' => $arr_order_id));
                $ordersbill = $ordersbill_obj->getList('logi_no', array('delivery_id' => $arr_order_id), 0, 1);

                $arr_order = '';
                $arr_order['stocknotice']['srcdocno'] = $orders[0]['delivery_bn'];
                $arr_order['stocknotice']['shop_id'] = $orders[0]['shop_id'];
                $arr_order['stocknotice']['total_amount'] = $orders[0]['total_amount'];
                $arr_order['stocknotice']['paytime'] = $orders[0]['paytime'];
                $arr_order['stocknotice']['relate_order_bn'] = $orders[0]['relate_order_bn'];
                $arr_order['stocknotice']['status'] = 'N';
                $arr_order['stocknotice']['stockdocno'] = $nowdate;
                $arr_order['stocknotice']['logi_no'] = $ordersbill[0]['logi_no'];

                //创建OMS备货通知单
                $stocknotice_model = $this->app->model('stocknotice');
                $res = $stocknotice_model->insert($arr_order['stocknotice']);

                $ax[$shop_bn]['id'][] = $res; //记录客户对于订单

                //修改OMS订单字段备货批号
                $orders_obj->db->exec("update sdb_wms_delivery set stockno = " . $nowdate . " , is_ship = 1 where  delivery_id in(" . $arr_order_id . ")");
                $deliver_order_ids = $orders_obj->getOrderIdByDeliveryId($arr_order_id);
                $deliver_order = $oms_order_obj->order_detail($deliver_order_ids[0]);
                $pmt_order = $deliver_order['pmt_order'] ? $deliver_order['pmt_order'] : 0;
                $p_pmt = 0;
                if ($pmt_order > 0) {
                    $p_pmt = round($pmt_order / count($orders_items), 2);
                }
                //整理物料信息
                if (!empty($f)) {

                    foreach ($orders_items as $v) {
                        if ($v) {
                            $ax[$shop_bn]['item'][$v['bn']]['item_id'][] = $v['item_id']; //记录客户对于订单
                            //print_r($v);
                            $unit = $sales_material->dump(array('material_bn' => $v['bn']), 'bm_id');
                            $unit = $sales_material_ext->dump(array('bm_id' => $unit['bm_id']), 'unit');
                            //print_r($unit);
                            $xml .= '<Cust>'
                                . '<CustAccount>' . $shop_bn . '</CustAccount>'
                                . '<ItemId>' . $v['bn'] . '</ItemId>'
                                . '<SalesPrice>' . round($v['price'] - $p_pmt, 2) . '</SalesPrice>'
                                . '<SalesQty>' . $v['number'] . '</SalesQty>'
                                . '<SalesUnit>' . $unit['unit'] . '</SalesUnit>'
                                . '<IsOnSales>no</IsOnSales>'
                                . '</Cust>';

                        }

                    }
                }
            }
        }
        //print_r($ax);
        //$xml = "<?xml version='1.0' encoding='UTF-8'><CustGroup><Cust><CustAccount>80042238</CustAccount><ItemId>DW3090G1001</ItemId><SalesPrice>99.9</SalesPrice><SalesQty>2</SalesQty><SalesUnit>件</SalesUnit><IsOnSales>no</IsOnSales></Cust></CustGroup>";
        $res = false;
        $msg = 'AX备货处理出现问题！';
        $respxml = '';
        if ($xml) {
            $action = 'CreateSalesOrder';
            $xml = "<?xml version='1.0' encoding='UTF-8'?><CustGroup>" . $xml . "</CustGroup>";
            //var_dump($xml);
            kernel::log($xml);
            $response = $this->HttpSoap('172.27.32.13', '8089', '/AXToOMs.asmx', 'http://tempuri.org/', $action, $xml);//测试
            //$response = $this->HttpSoap('172.27.32.7', '8812', '/AXToOMs.asmx', 'http://tempuri.org/', $action , $xml);//正式
            //过滤头部信息
            //print_r($response);
            kernel::log($response);
            $xml_pos = strpos($response, '<?xml');
            $response = substr($response, $xml_pos);
            //输出从RPC服务器端获取的信息
            $p = xml_parser_create();
            xml_parse_into_struct($p, $response, $vals, $index);
            xml_parser_free($p);
            kernel::log($response);

            //print_r($vals);
            if ($vals) {
                foreach ($vals as $v) {

                    if ($v['tag'] == 'CREATESALESORDERRESULT') {
                        $res = true;
                        $respxml = $v['value'];
                        break;
                    }
                    if ($v['tag'] == 'SOAP:TEXT') {
                        $res = false;
                        $msg = $v['value'];
                        break;
                    }
                }
            }

            if (strpos($respxml, 'error:') === 0) {
                $this->end(false, ($respxml));
            }
            /**
             * //测试
             * //$res = true;
             * $respxml = <<<xml
             * <?xml version='1.0' encoding='UTF-8'?>
             * <HV_WMSFGPickingRouteGroup>
             * <HV_WMSFGPickingRoute>
             * <fgpickingRouteID>FSN-11201523639</fgpickingRouteID>
             * <CustAccount>80042238</CustAccount>
             * <ItemId>DR3136L0301</ItemId>
             * <SalesId>SO1100028988</SalesId>
             * </HV_WMSFGPickingRoute>
             * </HV_WMSFGPickingRouteGroup>
             * xml;
             */
            if (!empty($respxml)) {
                $doc = new DOMDocument();
                $doc->loadXML($respxml);
                $items = $doc->getElementsByTagName('HV_WMSFGPickingRoute');
                foreach ($items as $key => $val) {
                    $fgpickingRouteID = $val->getElementsByTagName("fgpickingRouteID")->item(0)->nodeValue;
                    $CustAccount = $val->getElementsByTagName("CustAccount")->item(0)->nodeValue;
                    $SalesId = $val->getElementsByTagName("SalesId")->item(0)->nodeValue;
                    $ItemId = $val->getElementsByTagName("ItemId")->item(0)->nodeValue; //物料
                    $ax[$CustAccount]['axdocno'] = $SalesId; //AX订单号
                    $ax[$CustAccount]['item'][$ItemId]['shipdocno'] = $fgpickingRouteID; //出货单
                }
                //print_r($ax);
                if ($ax) {
                    foreach ($ax as $v) {
                        //修改OMS订单字段备货批号
                        if ($v['axdocno']) {//备货成功
                            //更新备货信息
                            $orders_obj->db->exec("update sdb_wms_stocknotice"
                                . " set axdocno = '" . $v['axdocno'] . "'  "
                                . " where id in(" . implode(',', $v['id']) . ")");

                            //更新物料发货单
                            if ($v['item']) {
                                foreach ($v['item'] as $iv) {
                                    if ($iv) {
                                        $orders_obj->db->exec("update sdb_wms_delivery_items"
                                            . " set shipdocno = '" . $iv['shipdocno'] . "'  "
                                            . " where item_id  in (" . implode(',', $iv['item_id']) . ")");
                                    }
                                }
                            }
                            $msg = '备货成功';
                        } else {
                            $orders_obj->db->exec("delete from sdb_wms_stocknotice"
                                . " where id in(" . implode(',', $v['id']) . ")");
                            $msg = '备货失败';
                        }

                    }

                }
            }
        } else {
            $msg = '无备货信息，请检查是否已经备货';
        }
        $this->end($res, ($msg));
    }

    /*
     * 分析状态
     */

    function analyseStatus($status, $type = 'normal')
    {
        $sku = kernel::single('base_component_request')->get_get('sku');
        $deliCfgLib = kernel::single('wms_delivery_cfg');
        if ($type == 'normal') {
            switch ($status) {
                case '':
                    $title = '全部';
                    $filter = array();
                    $filter['status'] = array('0', '3');
                    break;
                case 0:
                    if ($sku == 'single') {
                        $title = '单品打印';
                    } elseif ($sku == 'multi') {
                        $title = '多品打印';
                    } else {
                        $title = '待打印';
                    }

                    $btncombi = $deliCfgLib->btnCombi($sku);
                    switch ($btncombi) {
                        case '1_1':
                            $filter['todo'] = "1";
                            $filter['status'] = array('0');
                            $filter['process_status'] = array(0, 1);
                            break;
                        case '1_0':
                            $filter['todo'] = "2";
                            $filter['status'] = array('0');
                            $filter['process_status'] = array(0, 1);
                            break;
                        case '0_1':
                            $filter['todo'] = "3";
                            $filter['status'] = array('0');
                            $filter['process_status'] = array(0, 1);
                            break;
                        case '0_0':
                            $filter['todo'] = "4";
                            $filter['status'] = array('0');
                            $filter['is_ship'] = 'true';
                            $filter['is_form'] = 'true';
                            $filter['process_status'] = array(0, 1);
                            break;
                    }
                    break;
                case 1:
                    $title = '已打印';
                    $filter['process_status'] = 1;
                    $filter['status'] = array('0');
                    $btncombi_single = $deliCfgLib->btnCombi('single');
                    $btncombi_multi = $deliCfgLib->btnCombi('multi');
                    $btncombi_basic = $deliCfgLib->btnCombi();
                    $filter['print_finish'] = array(
                        '' => $btncombi_basic,
                        'single' => $btncombi_single,
                        'multi' => $btncombi_multi,
                    );
                    break;
                case 2:
                    $title = '未录入物流单号';
                    $filter['no_logi_no'] = true;
                    $filter['status'] = array('0');
                    break;
                case 3:
                    $title = '已校验';
                    $filter['process_status'] = 3;
                    $filter['status'] = array('0');
                    break;
                case 4:
                    $title = '未发货';
                    $filter['status'] = array('0');
                    break;
                case 5:
                    $title = '已发货';
                    $filter['status'] = 3;
                    break;
                case 6:
                    $title = '暂停列表';
                    $filter['status'] = 2;
                    break;
                case 7:
                    $title = '第三方已发货列表';
                    $filter['status'] = 3;
                    break;
            }
        } elseif ($type == 'refunded') {
            switch ($status) {
                case '':
                    $title = '未发货';
                    $filter['status'] = array('0');
                    break;
                case 1:
                    $title = '未发货';
                    $filter['status'] = array('0');
                    break;
                case 2:
                    $title = '已发货';
                    $filter['status'] = 3;
                    break;
            }
        }
        // 打印类型
        if ($sku) {
            if ($sku == 'single') {
                $filter['skuNum'] = 1;
            } elseif ($sku == 'multi') {
                $filter['skuNum|than'] = 1;
            }
        }

        //默认条件
        $filter['disabled'] = 'false';

        $schema = app::get('ome')->model('delivery')->schema;
        if (isset($_POST['status']) && $schema['columns']['status']['type'][$_POST['status']]) {
            $filter['status'] = $_POST['status'];
        }

        $filter['_title_'] = $title;

        return $filter;
    }

    function processFilter()
    {
        //来源于
        if ($_GET['from'] && $_GET['from'] == 'refunded') {
            //原样寄回
            $filter['type'] = 'reject';
            //TODO 可能通过getView()方法实现 （待升到1.3 moontools）
            $tmp = $this->analyseStatus($_GET['status'], 'refunded'); //判断为哪种列表
            $filter = array_merge($filter, $tmp);
        } else {
            //正常发货
            $filter['type'] = 'normal';
            //TODO 可能通过getView()方法实现 （待升到1.3 moontools）
            $tmp = $this->analyseStatus($_GET['status']); //判断为哪种列表
            $filter = array_merge($filter, $tmp);
        }
        //解析 view条件
        if ($_GET['logi_id']) {
            $filter['logi_id'] = urldecode($_GET['logi_id']);
        }
        if ($_POST['branch_id']){

            $filter['branch_id'] = $_POST['branch_id'];
        }

        if($_POST['area'])
        {
            $filter['ship_province']=$_POST['area'];
        }

        /*
         * 可扩展条件
         */

        return $filter;
    }

    /**
     * 处理发货单ID
     */
    function processDeliveryId()
    {
        $delivery_ids = $_REQUEST['delivery_id'];

       if(base_request::get_remote_addr()=='211.161.240.203')
        {
	   error_log(var_export(array($delivery_ids),true),3,PUBLIC_DIR."/lakers12-22.log");
	}

	$isSelectAll = $_REQUEST['isSelectedAll'];
        $filter = $this->processFilter();

        $printShip = in_array($_GET['act'], array('toPrintShip', 'toPrintExpre')) ? true : false;

        $logi = array();

        $dlyObj = app::get('wms')->model('delivery');
        $dlyCheckLib = kernel::single('wms_delivery_check');

        if ($isSelectAll == '_ALL_') {
            //所有数据
            $ids = $dlyObj->getList('delivery_id,logi_id,branch_id', $filter, 0, -1);
            $dly_ids = array();
            $branch = array();
            if ($ids) {
                foreach ($ids as $id) {
                    if ($printShip) {
                        $logi[$id['logi_id']]++;
                    }

                    $dly_ids[] = $id['delivery_id'];
                    $branch[$id['branch_id']] = $id['delivery_id'];
                }
                if (count($logi) > 1)
                    exit("当前系统不支持同时打印两种不同快递类型的单据，请重新选择后再试。");
                if (count($branch) > 1)
                    exit("当前系统不支持同时打印两个仓库的单据，请重新选择后再试。");
                return $dly_ids;
            }
            exit("无数据");
        } else {
            $delivery_ids = array_filter($delivery_ids); //去除值 为空，null，FALSE的key和value
            //选择的数据
            if ($delivery_ids) {
                if (is_array($delivery_ids)) {
                    $filter_['delivery_id'] = $delivery_ids;
                    $ids = $dlyObj->getList('delivery_id,logi_id,branch_id', $filter_, 0, -1);
                    $dly_ids = array();
                    $branch = array();
                    if ($ids) {
                        foreach ($ids as $id) {
                            if ($printShip) {
                                $logi[$id['logi_id']]++;
                            }

                            //$dly_ids[] = $id['delivery_id'];
                            $branch[$id['branch_id']] = $id['delivery_id'];
                        }

                        if (count($logi) > 1) {
                            exit("当前系统不支持同时打印两种不同快递类型的单据，请重新选择后再试。");
                        }

                        if (count($branch) > 1) {
                            exit("当前系统不支持同时打印两个仓库的单据，请重新选择后再试。");
                        }
                        return $delivery_ids;
                    }
                    exit("无数据");
                } else {
                    return array($delivery_ids);
                }
            } else {
                exit("请选择数据");
            }
        }
    }

    /*
     * 联合打印
     */
    function toMergePrint()
    {
        $_err = 'false';
        //单品、多品标识
        $sku = kernel::single('base_component_request')->get_get('sku');
        $sku = $sku ? $sku : '';

        $now_print_type = 'merge';

        //获取当前待打印的发货单过滤条件
        $filter_condition = $this->getPreparePrintIds();

        $PrintLib = kernel::single('wms_delivery_print');
        $print_data = $PrintLib->getPrintDatas($filter_condition, $now_print_type, $sku, true, $msg);
        if (isset($msg['error_msg']) && $msg['error_msg']) {
            $this->headerErrorMsgDisply($msg['error_msg']);
        }

        if (isset($msg['warn_msg']) && $msg['warn_msg']) {
            $this->message($msg['warn_msg']);
            exit;
        }

        $PrintMergeLib = kernel::single('wms_delivery_print_merge');
        $format_data = $PrintMergeLib->format($print_data, $sku, $_err);
        $this->pagedata = $format_data;

        //发货单拼接
        $this->pagedata['vid'] = implode(',', $print_data['ids']);
        //是否存在错误信息
        $this->pagedata['err'] = $_err;
        $this->pagedata['errBns'] = $print_data['errBns'];
        $this->pagedata['errIds'] = $print_data['errIds'];
        $this->pagedata['errInfo'] = $print_data['errInfo'];
        $this->pagedata['idents'] = $print_data['identInfo']['items'];
        $this->pagedata['ident'] = join(',', $print_data['identInfo']['idents']);
        $this->pagedata['base_dir'] = kernel::base_url();
        $this->pagedata['time'] = date("Y-m-d H:i:s");
        $this->pagedata['title'] = '联合打印单打印';
        $this->pagedata['sku'] = $sku;

        kernel::single('ome_print_otmpl')->printOTmpl($_GET['otmplId'], $now_print_type, $this);
    }

    /**
     * 打印发货单
     *
     */
    function toPrintMerge()
    {
        $_err = 'false';

        # 多品单品标识
        $sku = kernel::single('base_component_request')->get_get('sku');
        $sku = $sku ? $sku : '';

        $now_print_type = 'delivery';

        //获取当前待打印的发货单过滤条件
        $filter_condition = $this->getPreparePrintIds();

        $PrintLib = kernel::single('wms_delivery_print');
        $print_data = $PrintLib->getPrintDatas($filter_condition, $now_print_type, $sku, true, $msg);
        if (isset($msg['error_msg']) && $msg['error_msg']) {
            $this->headerErrorMsgDisply($msg['error_msg']);
        }

        if (isset($msg['warn_msg']) && $msg['warn_msg']) {
            $this->message($msg['warn_msg']);
            exit;
        }

        $PrintDlyLib = kernel::single('wms_delivery_print_delivery');
        $format_data = $PrintDlyLib->format($print_data, $sku, $_err);
        $this->pagedata = $format_data;

        $this->pagedata['errBns'] = $print_data['errBns'];
        $this->pagedata['sku'] = $sku;
        $this->pagedata['err'] = $_err;
        $this->pagedata['idents'] = $print_data['identInfo']['items'];
        $this->pagedata['ident'] = join(',', $print_data['identInfo']['idents']);
        $this->pagedata['errIds'] = $print_data['errIds'];
        $this->pagedata['errInfo'] = $print_data['errInfo'];
        $this->pagedata['base_dir'] = kernel::base_url();
        $this->pagedata['time'] = date("Y-m-d H:i:s");
        $this->pagedata['title'] = '发货单打印';

        //改用新打印模板机制 chenping
        kernel::single('ome_print_otmpl')->printOTmpl($_GET['otmplId'], $now_print_type, $this);
    }

    /**
     * 新版打印发货单
     *
     */
    function toPrintMergeNew()
    {
        $_err = 'false';

        //多品单品标识
        $sku = kernel::single('base_component_request')->get_get('sku');
        $sku = $sku ? $sku : '';

        $now_print_type = 'delivery';
        $now_print_mode = 'new';

        //获取当前待打印的发货单过滤条件
        $filter_condition = $this->getPreparePrintIds();

        $PrintLib = kernel::single('wms_delivery_print');
        $print_data = $PrintLib->getPrintDatas($filter_condition, $now_print_type, $sku, true, $msg);

        if (isset($msg['error_msg']) && $msg['error_msg']) {
            $this->headerErrorMsgDisply($msg['error_msg']);
        }

        if (isset($msg['warn_msg']) && $msg['warn_msg']) {
            $this->message($msg['warn_msg']);
            exit;
        }

        $PrintDlyLib = kernel::single('wms_delivery_print_newdelivery');
        $format_data = $PrintDlyLib->format($print_data, $sku, $_err);

        $deliveryDataObj = kernel::single('wms_print_data_newdelivery');
        foreach ($format_data['items'] as $delivery) {
            $logi_name = $delivery['logi_name'];
            $allItems[] = $deliveryDataObj->getElectronOrder($delivery);
        }
        $jsondata = '';
        if ($allItems) {
            $jsondata = $PrintDlyLib->arrayToJson($allItems);

        }


        $this->pagedata['err'] = $_err;
        $this->pagedata['title'] = '发货单打印';
        //物流公司

        $this->pagedata['vid'] = implode(',', $print_data['ids']);
        $this->pagedata['logi_name'] = $logi_name;
        //打印数量
        $this->pagedata['count'] = count($allItems);
        //随机数
        $this->pagedata['uniqid'] = uniqid();
        //组织控件打印数据
        $this->pagedata['jsondata'] = $jsondata;
        $this->pagedata['totalPage'] = count($allItems);

        ome_print_controltmpl::instance($now_print_type, $this)->printOTmpl($_GET['otmplId']);
    }

    /**
     * 打印新版本数据结构重组
     *
     * @return void
     * @author chenping<chenping@shopex.cn>
     **/
    private function format_print_delivery($delivery)
    {
        $format_delivery_items = array();
        foreach ($delivery['delivery_items'] as $key => $value) {
            $format_delivery_items[$value['item_id']] = $value;
        }

        $orderObjModel = app::get('ome')->model('order_objects');
        $orderObjList = $orderObjModel->getList('*', array('order_id' => array_keys($delivery['delivery_order'])));
        $format_order_objects = array();
        foreach ($orderObjList as $key => $value) {
            $format_order_objects[$value['obj_id']] = $value;
        }

        $orderItemModel = app::get('ome')->model('order_items');
        $orderItemList = $orderItemModel->getList('item_id,obj_id,bn,price,pmt_price,sale_price,amount,weight,nums as number', array('order_id' => array_keys($delivery['delivery_order']), 'delete' => 'false'));
        $format_order_items = array();
        foreach ($orderItemList as $key => $value) {
            $format_order_items[$value['item_id']] = $value;
        }

        $omepkgObj = app::get('omepkg')->model('pkg_goods');
        //是否打印前端商品名称
        $deliCfgLib = kernel::single('wms_delivery_cfg');
        $is_print_front = (1 == $deliCfgLib->getValue('wms_delivery_is_printdelivery', $sku)) ? true : false;

        $deliItemDetailModel = app::get('ome')->model('delivery_items_detail');
        $deliItemDetailList = $deliItemDetailModel->getList('*', array('delivery_id' => $delivery['delivery_id']));
        $data = array();
        foreach ($deliItemDetailList as $key => $value) {
            $order_object = $format_order_objects[$value['order_obj_id']];
            $order_item = $format_delivery_items[$value['delivery_item_id']];
            if (!$order_item) {
                continue;
            }

            $order_item = array_merge($order_item, $format_order_items[$value['order_item_id']]);

            if (isset($data[$order_object['obj_type']][$order_object['bn']])) {
                $obj_id_list = $data[$order_object['obj_type']][$order_object['bn']]['obj_id_list'];
                if (!in_array($order_object['obj_id'], $obj_id_list)) {
                    $obj_id_list[] = $order_object['obj_id'];
                    $data[$order_object['obj_type']][$order_object['bn']]['obj_id_list'] = $obj_id_list;
                    $data[$order_object['obj_type']][$order_object['bn']]['quantity'] += $order_object['quantity'];
                    $data[$order_object['obj_type']][$order_object['bn']]['amount'] += $order_object['amount'];
                    $data[$order_object['obj_type']][$order_object['bn']]['sale_price'] += $order_object['sale_price'];
                    $data[$order_object['obj_type']][$order_object['bn']]['pmt_price'] += $order_object['pmt_price'];
                }

                if (isset($data[$order_object['obj_type']][$order_object['bn']]['order_items'][$value['bn']])) {
                    $data[$order_object['obj_type']][$order_object['bn']]['order_items'][$value['bn']]['number'] += $order_item['number'];
                    $data[$order_object['obj_type']][$order_object['bn']]['order_items'][$value['bn']]['pmt_price'] += $order_item['pmt_price'];
                    $data[$order_object['obj_type']][$order_object['bn']]['order_items'][$value['bn']]['sale_price'] += $order_item['sale_price'];
                    $data[$order_object['obj_type']][$order_object['bn']]['order_items'][$value['bn']]['amount'] += $order_item['amount'];
                } else {
                    $data[$order_object['obj_type']][$order_object['bn']]['order_items'][$value['bn']] = $order_item;
                }
            } else {
                $order_object['obj_id_list'][] = $order_object['obj_id'];
                $data[$order_object['obj_type']][$order_object['bn']] = $order_object;
                $data[$order_object['obj_type']][$order_object['bn']]['order_items'][$value['bn']] = $order_item;

                if ($order_object['obj_type'] == 'pkg') {
                    $pkg = $omepkgObj->getlist('name', array('pkg_bn' => $order_object['bn']), 0, 1);
                    if (!$is_print_front) {
                        $data[$order_object['obj_type']][$order_object['bn']]['product_name'] = $pkg[0]['name'];
                        $data[$order_object['obj_type']][$order_object['bn']]['name'] = $pkg[0]['name'];
                    } else {
                        $data[$order_object['obj_type']][$order_object['bn']]['product_name'] = $order_object['name'];
                    }
                }
            }
        }
        $delivery['delivery_items'] = $data;

        return $delivery;
    }

    /**
     * 打印快递单
     *
     * 修改 加了一个补打快递单的开关 wujian@shopex.cn 2012年3月14日
     */
    function toPrintShip($afterPrint = true)
    {
        $basicMaterialLib = kernel::single('material_basic_material');

        $dlyObj = app::get('wms')->model('delivery');
        $dlyBillObj = app::get('wms')->model('delivery_bill');
        $orderObj = app::get('ome')->model('orders');
        $dly_corpObj = app::get('ome')->model('dly_corp');

        $order_sellagentObj = app::get('ome')->model('order_selling_agent');
        $omeDeliveryObj = app::get('ome')->model('delivery');
        $_err = 'false';

        //$ids = $this->reocessDeliveryId();
        $ids = $this->processDeliveryId();

        //打印排序
        if ($afterPrint) {
            $ids = $dlyObj->printOrderByByIds($ids);
        }

        //批次号 by sy
        if ($afterPrint) {
            $idents = $this->_getPrintQueue($ids);
        }

        $idsAll = $this->parsePrintIds($ids);

        $allItems = array();

        $ids = $idsAll['ids'];

        # 单品、多品标识
        $sku = kernel::single('base_component_request')->get_get('sku');
        $sku = $sku ? $sku : '';

        # 给发货单加上相应配置项
        if ($ids) {
            $this->updateDeliCfg($ids, $sku);
        }
        //备注显示方式
        $markShowMethod = app::get('ome')->getConf('ome.order.mark');
        $express_company_no = '';

        if ($ids) {
            $name = implode(',', $ids);
        }

        if ($ids) {
            //sort($ids);

            $idds = $ids;
            unset($ids);
            $rows = array();
            foreach ($idds as $id) {
                $outer_id = $dlyObj->getOuterIdById($id);
                $data = $omeDeliveryObj->dump($outer_id, '*', array('delivery_order' => array('*'), 'delivery_items' => array('*')));
                $data['wms_delivery_id'] = $id;
                if ($data['parent_id'] != 0) {
                    $_err = 'true';
                    continue;
                }
                $num = 0;
                $err = '';
                if ($data) {
                    //批次号
                    $allItems[$data['delivery_id']] = $data;


                    foreach ($data['delivery_items'] as $k => $i) {
                        $num += $i['number'];

                        $bMaterialRow = $basicMaterialLib->getBasicMaterialExt($i['product_id']);

                        $data['delivery_items'][$k]['product_name'] = $bMaterialRow['material_name'];
                        $data['delivery_items'][$k]['addon'] = $bMaterialRow['specifications'];
                        $data['delivery_items'][$k]['bn_dbvalue'] = $data['delivery_items'][$k]['bn'];
                    }
                    $o_bn = array();
                    $mark_text = array();
                    $custom_mark = array();
                    $total_amount = array();
                    foreach ($data['delivery_order'] as $v) {
                        $order = $orderObj->dump($v['order_id'], 'order_bn,mark_text,custom_mark,total_amount');

                        if ($order['mark_text']) {
                            $mark = unserialize($order['mark_text']);
                            if (is_array($mark) || !empty($mark)) {
                                if ($markShowMethod == 'all') {
                                    foreach ($mark as $im) {
                                        $mark_text[] = $im['op_content'];
                                    }
                                } else {
                                    $mark = array_pop($mark);

                                    $mark_text[] = $mark['op_content'];
                                }
                            }
                        }

                        if ($order['custom_mark']) {
                            $custommark = unserialize($order['custom_mark']);
                            if (is_array($custommark) || !empty($custommark)) {
                                if ($markShowMethod == 'all') {
                                    foreach ($custommark as $im) {
                                        $custom_mark[] = $im['op_content'];
                                    }
                                } else {
                                    $mark = array_pop($custommark);
                                    $custom_mark[] = $mark['op_content'];
                                }
                            }
                        }
                        $o_bn[] = $order['order_bn'];
                        $total_amount[] = $order['total_amount'];
                    }

                    $shop = $omeDeliveryObj->getShopInfo($data['shop_id']);
                    //发货人信息
                    $shipper_detail = $orderObj->dump(array('order_id' => $v['order_id']), 'consigner_name,consigner_area,consigner_addr,consigner_zip,consigner_email,consigner_mobile,consigner_tel');
                    $row = $dly_corpObj->dump($data['logi_id'], 'prt_tmpl_id,type');
                    $data['prt_tmpl_id'] = $row['prt_tmpl_id'];
                    $data['shopinfo'] = $shop;
                    $data['order_memo'] = implode(',', $mark_text);
                    $data['order_custom'] = implode(',', $custom_mark);
                    $data['order_count'] = $num;
                    $data['order_bn'] = implode(',', $o_bn);
                    $data['order_total_amount'] = implode(',', $total_amount);
                    //去除多余的三级区域
                    $reg = preg_quote(trim($data['consignee']['province']));
                    if (!empty($data['consignee']['city'])) {
                        $reg .= '.*?' . preg_quote(trim($data['consignee']['city']));
                    }
                    if (!empty($data['consignee']['district'])) {
                        $reg .= '.*?' . preg_quote(trim($data['consignee']['district']));
                    }

                    $data['consignee']['addr'] = preg_replace('/' . $reg . '/is', '', $data['consignee']['addr']);

                    //快递公式
                    if (!$express_company_no) {
                        $express_company_no = strtoupper($row['type']);
                        $logi_name = $data['logi_name'];
                    }
                    $rows['delivery'][] = $data;

                    //快递单关联程序,取wms信息
                    $itm['delivery_id'] = $id;
                    $deliveryInfo = $dlyObj->dump($id);
                    $itm['delivery_bn'] = $deliveryInfo['delivery_bn'];
                    $itm['consignee']['name'] = $deliveryInfo['consignee']['name'];
                    $idd[] = $itm;

                    $dlyBillInfo = $dlyBillObj->dump(array('delivery_id' => $id, 'type' => 1), 'logi_no');
                    $logid[$id] = $dlyBillInfo['logi_no'];
                    $ids[] = $id;
                } else {
                    $_err = 'true';
                }
            }

        }

        $rows['dly_tmpl_id'] = $data['prt_tmpl_id'];
        $rows['order_number'] = count($ids);
        $rows['name'] = $name;
        //物流公司标识
        $this->pagedata['print_logi_id'] = $data['logi_id'];
        //商品名称和规格取前台,是合并发货单,取第一个订单的货品名称
        $deliCfgLib = kernel::single('wms_delivery_cfg');
        $is_print_front = (1 == $deliCfgLib->getValue('wms_delivery_is_printship')) ? true : false;

        if ($ids && $is_print_front) {
            $arrPrintProductName = $omeDeliveryObj->getPrintProductName($ids);
            if (!empty($arrPrintProductName)) {
                foreach ($rows['delivery'] as $k => $row) {
                    foreach ($row['delivery_items'] as $k2 => $v) {
                        $bncode = md5($row['shop_id'] . $v['bn']);
                        $row['delivery_items'][$k2]['product_name'] = $arrPrintProductName[$bncode]['name'];
                        //$row['delivery_items'][$k2]['name'] = $arrPrintProductName[$v['bn']]['name'];
                        $row['delivery_items'][$k2]['addon'] = $arrPrintProductName[$bncode]['addon'];
                        $row['delivery_items'][$k2]['spec_info'] = $arrPrintProductName[$bncode]['addon'];
                        $row['delivery_items'][$k2]['store_position'] = $arrPrintProductName[$bncode]['store_position'];
                    }
                    $rows['delivery'][$k] = $row;
                }
            }
        } elseif ($ids) {
            // 货位的获取
            $tmp_product_ids = array();
            foreach ($rows['delivery'] as $k => $row) {
                foreach ($row['delivery_items'] as $k2 => $v) {
                    $tmp_product_ids[] = $v['product_id'];

                    $bpro_key = $row['branch_id'] . $v['product_id'];
                    $rows['delivery'][$k]['delivery_items'][$k2]['store_position'] = &$bpro[$bpro_key];
                }
            }
            // 货品货位有关系
            $bppModel = app::get('ome')->model('branch_product_pos');
            $bppList = $bppModel->getList('product_id,pos_id,branch_id', array('product_id' => $tmp_product_ids));

            // 如果货位存在
            if ($bppList) {
                // 货位信息
                $tmp_pos_ids = array();
                foreach ($bppList as $key => $value) {
                    $tmp_pos_ids[] = $value['pos_id'];
                }

                $posModel = app::get('ome')->model('branch_pos');
                $posList = $posModel->getList('pos_id,branch_id,store_position', array('pos_id' => $tmp_pos_ids));

                $newPosList = array();
                foreach ($posList as $key => $value) {
                    $bpos_key = $value['branch_id'] . $value['pos_id'];

                    $bpos[$bpos_key] = $value['store_position'];
                }
                unset($posList);

                foreach ($bppList as $key => $value) {
                    $bpro_key = $value['branch_id'] . $value['product_id'];
                    $bpos_key = $value['branch_id'] . $value['pos_id'];
                    $bpro[$bpro_key] = $bpos[$bpos_key];
                }
                unset($bppList);
            }
        }

        $delivery_cfg = app::get('wms')->getConf('wms.delivery.status.cfg');
        #检测是否开启打印捆绑商品按钮
        $delivey_order = array();
        if ($delivery_cfg['set']['print_pkg_goods']) {
            foreach ($rows['delivery'] as $key => &$delivery) {
                #获取发货单上有捆绑商品item_id
                $pkg_item_id = $orderObj->getPkgItemId($delivery['delivery_id']);
                foreach ($pkg_item_id as $item_id) {
                    if (isset($delivery['delivery_items'][$item_id])) {
                        #删除这批打印数据中包含捆绑商品的货品信息
                        unset($delivery['delivery_items'][$item_id]);
                    }
                }
            }
        }
        if ($rows['delivery'])
            foreach ($rows['delivery'] as $val) {
                //获取快递单打印模板的servivce定义
                $data = array();
                foreach (kernel::servicelist('wms.service.template') as $object => $instance) {
                    if (method_exists($instance, 'getElementContent')) {
                        $tmp = $instance->getElementContent($val);
                    }
                    $data = array_merge($data, $tmp);
                }
                $mydata[] = $data;
            }

        $printTmpl = app::get('wms')->model('print_tmpl');

        $errDly = $dlyObj->getList('delivery_id,delivery_bn', array('delivery_id' => $idsAll['errIds']));
        foreach ($errDly as $val) {
            $errBns[$val['delivery_id']] = $val['delivery_bn'];
        }
        $this->pagedata['errBns'] = $errBns;

        $this->pagedata['data'] = addslashes($omeDeliveryObj->array2xml2($mydata, 'data'));
        $this->pagedata['order_number'] = $rows['order_number'];
        $this->pagedata['prt_tmpl'] = $printTmpl->dump($rows['dly_tmpl_id'], 'prt_tmpl_width,prt_tmpl_offsety,prt_tmpl_offsetx,prt_tmpl_height,prt_tmpl_data,file_id');
        /* 修改的地方 */
        if ($this->pagedata['prt_tmpl']['file_id']) {
            $this->pagedata['tmpl_bg'] = 'index.php?app=wms&ctl=admin_delivery_print&act=showPicture&p[0]=' . $this->pagedata['prt_tmpl']['file_id'];
        }
        $this->pagedata['err'] = $_err;
        $this->pagedata['vid'] = $rows['name'];

        //批次号
        $this->pagedata['allItems'] = $allItems;
        $this->pagedata['idents'] = $idents['items'];
        $this->pagedata['ident'] = join(',', $idents['idents']);
        $this->pagedata['errIds'] = $idsAll['errIds'];
        $this->pagedata['errInfo'] = $idsAll['errInfo'];
        $items = array();
        foreach ($rows['delivery'] as $row) {
            $items[$row['delivery_id']] = $row;
        }
        $this->pagedata['items'] = $items;
        $this->pagedata['sku'] = $sku;//单品 多品标识
        $this->pagedata['dpi'] = 96;
        $this->pagedata['count'] = sizeof($ids);
        $this->pagedata['ids'] = $ids;
        $this->pagedata['idd'] = $idd;
        $this->pagedata['logid'] = $logid;
        $this->pagedata['logi_name'] = $logi_name;

        $this->pagedata['express_company_no'] = $express_company_no;
        $this->pagedata['base_dir'] = kernel::base_url();
        $this->pagedata['title'] = '快递单打印';

        if (!$afterPrint) {
            $this->pagedata['b_id'] = $_REQUEST['b_id'];

            $dlyBillObj = app::get('wms')->model('delivery_bill');
            $billFilter = array(
                'b_id' => $_REQUEST['b_id'],
            );
            $this->pagedata['bill_logi_no'] = $dlyBillObj->getList('b_id,logi_no', $billFilter);
            $this->pagedata['delibery_bill_flag'] = 'delibery_bill_flag';
        }

        /**
         *新增新版本打印
         */
        //获取打印版本配置信息
        $logicfg = kernel::single('ome_print_logicfg')->getLogiCfg();

        if ($logicfg[$express_company_no]) {
            $logiVersionFlag = 1;
            $this->pagedata['logicfg'] = $logicfg[$express_company_no];
            $print_logi_version = app::get('ome')->getConf('print_logi_version_' . $this->pagedata['print_logi_id']);

            $this->pagedata['print_logi_version'] = intval($print_logi_version);
        }
        $this->pagedata['logiVersionFlag'] = $logiVersionFlag;
        $params = array('order_bn' => $o_bn);
        wms_print_tmpl_express::instance($express_company_no, $this)->setParams($params)->getTmpl();

    }

    /**
     *
     * 补打物流单
     */
    public function addPrintShip()
    {
        if (count($_REQUEST['b_id']) > 0) {
            $this->addPrintShipNoData();
            exit;
        }
        $num = $_REQUEST['num'];
        $str = $_REQUEST['delivery_id'];

        //写入日志
        $opObj = app::get('ome')->model('operation_log');
        $opObj->write_log('delivery_bill_print@wms', $_REQUEST['delivery_id'], '补打快递单(' . $num . ')份');

        //增加子数据
        //更新主物流单delivery中logi_number,delivery_logi_number信息
        $dlyObj = app::get('wms')->model('delivery');
        $dlyBillObj = app::get('wms')->model('delivery_bill');

        //京东面单补打
        $channel_info = kernel::single('wms_delivery_print_ship')->getWaybillType($str);
        $logi_no_fill = false;
        $logi_no_total = 1;
        if (in_array($channel_info['channel_type'], array('360buy'))) {
            $delivery_data = $dlyBillObj->dump(array('delivery_id' => $str, 'type' => '1'), 'logi_no');
            $logi_no_fill = true;
            $delivery_num = $dlyBillObj->count(array('delivery_id' => $str, 'type' => '2'));
            if ($delivery_num > 0) {

                $dlyBillObj->db->exec("DELETE FROM sdb_wms_delivery_bill WHERE delivery_id=" . $str . " AND `type`='2'");
            }
            $num += $delivery_num;

        }
        //京东
        $logi_no_num = 1;
        $logi_no_total += $num;
        for ($i = 0; $i < $num; $i++) {
            $data = array('delivery_id' => $str, 'create_time' => time(), 'type' => 2);
            if ($logi_no_fill) {
                $logi_no_num++;
                $data['logi_no'] = $delivery_data['logi_no'] . '-' . $logi_no_num . '-' . $logi_no_total . '-';
            }
            $b_id[] = $dlyBillObj->insert($data);
        }

        $delivery = app::get('wms')->model('delivery');
        $logi_number = $dlyBillObj->count(array('delivery_id' => $str));
        $sql = "update sdb_wms_delivery set logi_number=" . $logi_number . " where delivery_id=" . $str;
        $delivery->db->exec($sql);


        $_REQUEST['b_id'] = $b_id;
        $_REQUEST['delivery_id'] = array();
        for ($i = 0; $i < $num; $i++) {
            $_REQUEST['delivery_id'][] = $str;
        }

        //屏蔽toPrintShip 打印排序 和 批次号
        if (app::get('logisticsmanager')->is_installed()) {
            $this->toPrintExpre(false);
        } else {
            $this->toPrintShip(false);
        }
    }

    /**
     *
     * 补打物流单(无需更新数据)
     */
    public function addPrintShipNoData()
    {
        if (is_array($_REQUEST['b_id'])) {
            if (count($_REQUEST['b_id']) == 1) {
                $tmp_str = $_REQUEST['b_id'][0];
                unset($_REQUEST['b_id']);
                $_REQUEST['b_id'] = $tmp_str;
            } else {
                $tmp_arr = $_REQUEST['b_id'];
                unset($_REQUEST['b_id']);
                foreach ($tmp_arr as $k => $val) {
                    $_REQUEST['b_id'] .= $val . ",";
                }
                $_REQUEST['b_id'] = substr($_REQUEST['b_id'], 0, -1);
            }
        }
        $str = $_REQUEST['delivery_id'];
        $b_id = explode(',', $_REQUEST['b_id']);

        $dlyObj = app::get('wms')->model('delivery');
        $delivery_bill = app::get('wms')->model('delivery_bill');

        $flag = $dlyObj->dump(array('delivery_id' => $str));
        $filter = array(
            'b_id' => $b_id,
            'delivery_id' => $str
        );
        $datanum = $delivery_bill->count($filter);
        if (count($b_id) > $datanum || !$flag) {
            die('错误');
        }
        $num = $datanum;
        $_REQUEST['b_id'] = $b_id;
        $_REQUEST['delivery_id'] = array();
        for ($i = 0; $i < $num; $i++) {
            $_REQUEST['delivery_id'][] = $str;
        }

        //屏蔽toPrintShip 打印排序 和 批次号
        //var_dump($_REQUEST);
        if (app::get('logisticsmanager')->is_installed()) {
            $this->toPrintExpre(false);
        } else {
            $this->toPrintShip(false);
        }
    }

    /*
     * 关联物流单号
     * wujian@shopex.cn
     * 2012年3月19日
     */
    function insertAfterExpress()
    {
        $arr = $_POST;
        $arrc = count($arr["id"]);
        $uniquec = count(array_unique($arr["id"]));
        $opObj = app::get('ome')->model('operation_log');


        if ($arrc > $uniquec) {
            echo '物流号中有重复的值';
        } else {
            //保存对应物流公司所用打印版本

            $flag = true;
            $delivery_bill = app::get('wms')->model('delivery_bill');
            $delivery = app::get('ome')->model('delivery');
            foreach ($arr["id"] as $key => $value) {
                $filter['logi_no'] = $value;
                $deliveryCount = $delivery->count($filter);
                $deliveryBillCount = $delivery_bill->count($filter);
                if ($deliveryCount > 0 || $deliveryBillCount > 0) {
                    echo '已有此物流号:' . $value;
                    $flag = false;
                    die;
                }
            }
            if ($flag) {
                app::get('ome')->setConf('print_logi_version_' . $arr['print_logi_id'], intval($arr['logi_version']));

                foreach ($arr["id"] as $key => $value) {
                    $delivery_bill->update(array("logi_no" => $value), array('b_id' => $key));
                    $dlybillinfo = $delivery_bill->dump(array('b_id' => $key));
                    $logstr = '录入快递单号:' . $value;
                    $opObj->write_log('delivery_bill_add@ome', $dlybillinfo['delivery_id'], $logstr);
                }
                echo 'SUCC';
            }
        }
    }

    /**
     * 批量更改物流
     *
     * @param void
     * @return void
     */
    function toChangeDly()
    {
        //如果是全选的情况下，该功能代码还未兼容
        $ids = $_POST['delivery_id'];
        if (empty($ids)) {
            die('没有选择任何可操作的发货单。');
        }

        $dlyCrop = app::get('ome')->model('dly_corp')->getList('corp_id, name, type, is_cod, weight', array('disabled' => 'false', 'd_type' => '1'), 0, -1, 'weight DESC');

        $deliveryObj = app::get('wms')->model('delivery');
        $branchObj = app::get('ome')->model('branch');

        $deliverys = $deliveryObj->getList('delivery_id, branch_id, delivery_bn', array('delivery_id' => $ids));
        $branchDatas = array();
        foreach ($deliverys as $delivery) {
            $branchDatas[$delivery['branch_id']]['delivery'][$delivery['delivery_id']] = $delivery['delivery_bn'];
            $branchDatas[$delivery['branch_id']]['count']++;
        }
        foreach ($branchDatas as $key => $branchData) {
            $branchDatas[$key]['branch'] = $branchObj->dump($key, 'branch_id,name');
            $branchDatas[$key]['corp'] = $branchObj->get_corp($key);
        }

        $this->pagedata['ids'] = join(',', $ids);
        $this->pagedata['branchDatas'] = $branchDatas;
        $this->pagedata['dlyCorp'] = $dlyCrop;
        $this->pagedata['orderCnt'] = count($ids);
        $this->display('admin/delivery/change_dly.html');
    }

    /**
     * 更换物流公司
     *
     */
    function doChangeDly()
    {
        $this->begin();
        $branchDatas = $_POST['branchData'];
        $corpObj = app::get('ome')->model('dly_corp');
        $deliveryObj = app::get('wms')->model('delivery');
        $deliveryBillObj = app::get('wms')->model('delivery_bill');
        $opObj = app::get('ome')->model('operation_log');
        $waybillObj = kernel::single('logisticsmanager_service_waybill');
        $wmsCommonLib = kernel::single('wms_common');
        $rows = $corpObj->getList();
        foreach ($rows as $val) {
            $corpList[$val['corp_id']] = $val;
        }
        foreach ($branchDatas as $branch) {
            $branch_id = $branch['branch'];
            $newCorp = $branch['newCorp'];
            $deliveryIds = $branch['delivery'];
            if (!$branch_id || !$newCorp || !$deliveryIds) {
                $this->end(false, '请确定新的物流！');
            } else {
                $corp = $corpObj->dump($newCorp);

                $data['logi_id'] = $newCorp;
                $data['logi_name'] = $corp['name'];
                $data['logi_no'] = null;
                $data['status'] = 0;
                $data['process_status'] = 0;

                //未校验，未发货的有效发货单才可以修改
                $filter['delivery_id'] = $deliveryIds;
                $filter['branch_id'] = $branch_id;
                $filter['status'] = 0;
                $filter['process_status'] = array(0, 1);

                // 获取WMS
                $wms_id = kernel::single('ome_branch')->getWmsIdById($branch_id);

                $logs = array();
                if (is_array($deliveryIds)) {
                    foreach ($deliveryIds as $did) {
                        $dly_data = $deliveryObj->dump($did);
                        $bill_data = $deliveryBillObj->dump(array('delivery_id' => $did, 'type' => '1'));
                        //回收电子面单
                        if ($bill_data['logi_no'] && $corpList[$dly_data['logi_id']]['tmpl_type'] == 'electron') {
                            //直连物流：EMS、申通、京东回收

                            $waybillObj->recycle_waybill($bill_data['logi_no'], $corpList[$dly_data['logi_id']]['channel_id'], $did);
                        }
                        //重新计算预估物流费
                        $arrArea = explode(':', $dly_data['consignee']['area']);
                        $area_id = $arrArea[2];
                        $price = $wmsCommonLib->getDeliveryFreight($area_id, $newCorp, $dly_data['net_weight']);

                        $logs[] = array('did' => $did, 'cnt' => '修改物流:' . $dly_data['logi_name'] . ' => ' . $corp['name'], 'outer_delivery_bn' => $dly_data['outer_delivery_bn'], 'delivery_cost_expect' => $price);
                    }
                }
                if ($deliveryObj->update($data, $filter)) {
                    //
                    $wmsLib=kernel::single('wms_receipt_delivery');
                     foreach ($logs as $log) {
                        //更新自有仓库发货单预估物流费
                        $deliveryObj->update(array('delivery_cost_expect' => $log['delivery_cost_expect']), array('delivery_id' => $log['did']));
                        $deliveryBillObj->update(array('logi_no' => null, 'delivery_cost_expect' => $log['delivery_cost_expect']), array('delivery_id' => $log['did'], 'type' => 1));
                        // 返回物流公司
                        $tmp_data = array(
                            'delivery_bn' => $log['outer_delivery_bn'],
                            'logi_id' => $newCorp,
                            'logi_name' => $corp['name'],
                            'logi_no' => null,
                            'delivery_cost_expect' => $log['delivery_cost_expect'],
                            'action' => 'updateDetail',
                        );
                        $res = kernel::single('wms_event_trigger_delivery')->doUpdate($wms_id, $tmp_data, true);
                        $wmsLib->getFlatFee($log['did']);
                        $opObj->write_log('delivery_logi@wms', $log['did'], $log['cnt']);
                    }
                }
            }
        }
        $this->end(true, '更换物流成功！');
    }

    /**
     * 更新发货单的打印状态
     *
     * @param string $type
     * @param int $dly_id
     *
     */
    function setPrintStatus()
    {
        $current_otmpl_name = $_POST['current_otmpl_name'] ? $_POST['current_otmpl_name'] : '默认';
        $type = $_POST['type'];
        $str_id = $_POST['str'];
        $dlyObj = app::get('wms')->model('delivery');
        $tmp_id = array_filter(explode(',', $str_id));

        if (!$this->_checkPrintQueue($tmp_id, $msg)) {
            echo $msg;
            exit;
        }

        $dlys = $dlyObj->getList('*', array('delivery_id' => $tmp_id), 0, -1);
        $dly = array();
        $arr_s = array(1, 2);

        foreach ($dlys as $k => $delivery) {
            if (in_array($delivery['status'], $arr_s)) {
                continue;
            }

            //if ($delivery['status'] == 'ready')  $dly[$k]['status'] = 'progress';
            $dly[$k]['delivery_id'] = $delivery['delivery_id'];
            $send_flag = false;
            switch ($type) {
                case 'express':
                    if (($delivery['print_status'] & 4) != 4) {
                        $send_flag = true;
                    }
                    $dly[$k]['print_status'] = $delivery['print_status'] | 4;
                    $dly[$k]['_log_'] = 'delivery_expre@wms';
                    $dly[$k]['_memo_'] = '快递单打印';
                    //如果是当当物流订单，将订单号更新为物流单号
                    $logi_id = $delivery['logi_id'];
                    $dly_corpObj = app::get('ome')->model('dly_corp');
                    $orderObj = app::get('ome')->model('orders');
                    $dly_corp = $dly_corpObj->dump($logi_id, 'type');
                    if ($dly_corp['type'] == 'DANGDANG') {
                        $orderIds = $dlyObj->getOrderIdByDeliveryId($delivery['delivery_id']);
                        if ($orderIds)
                            $ids = implode(',', $orderIds);
                        if ($orderIds)
                            foreach ($orderIds as $oid) {
                                $order = $orderObj->dump($oid, 'order_bn');
                                $order_bn = $order['order_bn'];
                            }
                        $dly[$k]['logi_no'] = $order_bn;
                    }
                    break;
                case 'stock':
                    if (($delivery['print_status'] & 1) != 1) {
                        $send_flag = true;
                    }
                    $dly[$k]['print_status'] = $delivery['print_status'] | 1;
                    $dly[$k]['_log_'] = 'delivery_stock@wms';
                    $dly[$k]['_memo_'] = "备货单打印（打印模板： $current_otmpl_name ）";
                    break;
                case 'delivery':
                    if (($delivery['print_status'] & 2) != 2) {
                        $send_flag = true;
                    }
                    $dly[$k]['print_status'] = $delivery['print_status'] | 2;
                    $dly[$k]['_log_'] = 'delivery_deliv@wms';
                    $dly[$k]['_memo_'] = "发货单商品信息打印（打印模板： $current_otmpl_name ）";
                    break;
                case 'stock_dly':
                    if ((($delivery['print_status'] & 1) != 1) && (($delivery['print_status'] & 2) != 2)) {
                        $send_flag = true;
                    }
                    $dly[$k]['print_status'] = $delivery['print_status'] | 1;
                    $dly[$k]['print_status'] = $dly[$k]['print_status'] | 2;
                    $dly[$k]['_isMerge_'] = true;
                    $dly[$k]['_log_'][0] = 'delivery_stock@wms';
                    $dly[$k]['_log_'][1] = 'delivery_deliv@wms';
                    $dly[$k]['_memo_'][0] = "备货单打印（打印模板： $current_otmpl_name ）";
                    $dly[$k]['_memo_'][1] = "发货单商品信息打印（打印模板： $current_otmpl_name ）";
                    break;
            }
        }
        $opObj = app::get('ome')->model('operation_log');
        foreach ($dly as $k => $v) {
            $_dly = $v;
            $dlyObj->save($_dly);

            $delivery = $dlyObj->dump($v['delivery_id'], 'outer_delivery_bn,print_status,process_status,branch_id');

            $print_status = true;
            $stock_print_status = true;
            $delie_print_status = true;
            $merge_print_status = true;
            //根据打印单据配置及当前状态判断发货单打印状态
            $deliCfgLib = kernel::single('wms_delivery_cfg');
            $checkStock = $deliCfgLib->analyse_btn_status('stock');
            if ($checkStock == true && ($delivery['print_status'] & 1) != 1) {
                $stock_print_status = false;
            }

            $checkDelie = $deliCfgLib->analyse_btn_status('delie');
            if ($checkDelie == true && ($delivery['print_status'] & 2) != 2) {
                $delie_print_status = false;
            }

            $checkMerge = $deliCfgLib->analyse_btn_status('merge');
            if ($checkMerge == true && ((($delivery['print_status'] & 1) != 1) || (($delivery['print_status'] & 2) != 2))) {
                $merge_print_status = false;
            }

            if (($delivery['print_status'] & 4) != 4) {
                $print_status = false;
            }

            if ($print_status || $stock_print_status || $delie_print_status || $merge_print_status) {
                if ($stock_print_status && $delie_print_status && $print_status && $merge_print_status) {
                    $tmp_status = $delivery['process_status'] | 1;
                    $data = array('process_status' => $tmp_status, 'delivery_id' => $v['delivery_id']);
                    $dlyObj->save($data);
                }


                //同步打印状态到oms
                $wms_id = kernel::single('ome_branch')->getWmsIdById($delivery['branch_id']);
                $data = array(
                    'delivery_bn' => $delivery['outer_delivery_bn'],
                    'stock_status' => ($delivery['print_status'] & 1) == 1 ? 'true' : 'false',
                    'deliv_status' => ($delivery['print_status'] & 2) == 2 ? 'true' : 'false',
                    'expre_status' => ($delivery['print_status'] & 4) == 4 ? 'true' : 'false',
                );
                $res = kernel::single('wms_event_trigger_delivery')->doPrint($wms_id, $data, true);
            }

            if ($v['_isMerge_']) {//联合打印
                foreach ($v['_log_'] as $key => $val) {
                    $opObj->write_log($val, $v['delivery_id'], $v['_memo_'][$key]);
                }
            } else {
                $opObj->write_log($v['_log_'], $v['delivery_id'], $v['_memo_']);
            }
        }

        echo 'true';
    }

    /**
     * 打印备货单
     *
     */
    function toPrintStock()
    {
        $_err = 'false';
        # 发货配置类型
        $sku = kernel::single('base_component_request')->get_get('sku');
        $sku = $sku ? $sku : '';

        $now_print_type = 'stock';

        //获取当前待打印的发货单过滤条件
        $filter_condition = $this->getPreparePrintIds();

        $PrintLib = kernel::single('wms_delivery_print');
        $print_data = $PrintLib->getPrintDatas($filter_condition, $now_print_type, $sku, true, $msg);
        if (isset($msg['error_msg']) && $msg['error_msg']) {
            $this->headerErrorMsgDisply($msg['error_msg']);
        }

        if (isset($msg['warn_msg']) && $msg['warn_msg']) {
            $this->message($msg['warn_msg']);
            exit;
        }

        $PrintStockLib = kernel::single('wms_delivery_print_stock');
        $format_data = $PrintStockLib->format($print_data, $sku, $_err);
        $this->pagedata = $format_data;

        $this->pagedata['errBns'] = $print_data['errBns'];
        $this->pagedata['err'] = $_err;
        $this->pagedata['allItems'] = $print_data['deliverys'];
        $this->pagedata['idents'] = $print_data['identInfo']['items'];
        $this->pagedata['ident'] = join(',', $print_data['identInfo']['idents']);
        $this->pagedata['errIds'] = $print_data['errIds'];
        $this->pagedata['errInfo'] = $print_data['errInfo'];
        $this->pagedata['time'] = date("Y-m-d H:i:s");
        $this->pagedata['sku'] = $sku;
        $this->pagedata['base_dir'] = kernel::base_url();
        $this->pagedata['title'] = '备货单打印';

        kernel::single('ome_print_otmpl')->printOTmpl($_GET['otmplId'], $now_print_type, $this);
    }

    /**
     * 备货单新版
     */
    function toPrintStockNew()
    {
        $_err = 'false';
        # 发货配置类型
        $sku = kernel::single('base_component_request')->get_get('sku');
        $sku = $sku ? $sku : '';

        $now_print_type = 'stock';

        //获取当前待打印的发货单过滤条件
        $filter_condition = $this->getPreparePrintIds();

        $PrintLib = kernel::single('wms_delivery_print');
        $print_data = $PrintLib->getPrintDatas($filter_condition, $now_print_type, $sku, true, $msg);
        if (isset($msg['error_msg']) && $msg['error_msg']) {
            $this->headerErrorMsgDisply($msg['error_msg']);
        }

        if (isset($msg['warn_msg']) && $msg['warn_msg']) {
            $this->message($msg['warn_msg']);
            exit;
        }

        $PrintStockLib = kernel::single('wms_delivery_print_stock');
        $format_data = $PrintStockLib->format($print_data, $sku, $_err);
        $this->pagedata = $format_data;

        //备货打印json数据
        $jsondata = $PrintStockLib->arrayToJson($format_data['rows'], $print_data['identInfo']['idents'], $this->pagedata);

        $this->pagedata['errBns'] = $print_data['errBns'];
        $this->pagedata['err'] = $_err;
        $this->pagedata['allItems'] = $print_data['deliverys'];
        $this->pagedata['idents'] = $print_data['identInfo']['items'];
        $this->pagedata['ident'] = join(',', $print_data['identInfo']['idents']);
        $this->pagedata['errIds'] = $print_data['errIds'];
        $this->pagedata['errInfo'] = $print_data['errInfo'];
        $this->pagedata['time'] = date("Y-m-d H:i:s");
        $this->pagedata['sku'] = $sku;
        $this->pagedata['base_dir'] = kernel::base_url();
        $this->pagedata['title'] = '备货单打印';
        $this->pagedata['jsondata'] = $jsondata;
        $this->pagedata['count'] = sizeof($print_data['ids']);
        $this->pagedata['totalPage'] = count($printData);

        ome_print_controltmpl::instance($now_print_type, $this)->printOTmpl($_GET['otmplId']);
    }

    /**
     * 录入快递单号
     *
     */
    function insertExpress()
    {
        if (empty($_POST['id'])) {
            exit("请录入快递单号");
        }
        $ids = $_POST['id'];
        $dlyObj = app::get('wms')->model('delivery');
        $deliveryBillObj = app::get('wms')->model('delivery_bill');
        $errmsg = '';
        $idds = array();

        if ($ids) {
            if (count($ids) > count(array_unique($ids))) {
                exit("快递单号有重复!");
            }

            foreach ($ids as $k => $i) {
                $i = $i ? trim($i) : null;
                $delivery = $dlyObj->dump($k);
                $bn = $delivery['delivery_bn'];
                $arr_s = array(1, 2, 3);
                if (in_array($delivery['status'], $arr_s)) {
                    $errmsg .= "发货单" . $bn . "相关信息不能修改\n";
                    unset($ids[$k]);
                }

                if (empty($i)) {
                    exit("物流单号不能为空，发货单为" . $bn);
                }

                if ($dlyObj->existExpressNo($i, $k)) {
                    exit("物流单号已存在，发货单为" . $bn);
                }
                $dlyBillInfo = $deliveryBillObj->dump(array('delivery_id' => $k, 'type' => 1), 'b_id');
                $idds[$k]['b_id'] = $dlyBillInfo['b_id'];
                $idds[$k]['branch_id'] = $delivery['branch_id'];
                $idds[$k]['outer_delivery_bn'] = $delivery['outer_delivery_bn'];
            }
        }

        $opObj = app::get('ome')->model('operation_log');
        if ($ids) {
            foreach ($ids as $key => $item) {
                $dlyLog = array();
                $item = $item ? trim($item) : null;
                $data['b_id'] = $idds[$key]['b_id'];
                $data['logi_no'] = $item;
                $data['type'] = 1;

                if ($item && $key) {
                    $deliveryBillObj->save($data);

                    //信息变更更新到oms
                    $wms_id = kernel::single('ome_branch')->getWmsIdById($idds[$key]['branch_id']);
                    $tmp_data = array(
                        'delivery_bn' => $idds[$key]['outer_delivery_bn'],
                        'logi_no' => $data['logi_no'],
                        'action' => 'addLogiNo',
                    );
                    $res = kernel::single('wms_event_trigger_delivery')->doUpdate($wms_id, $tmp_data, true);
                }

                $opObj->write_log('delivery_logi_no@wms', $key, '录入快递单号:' . $item);
            }
        }

        app::get('ome')->setConf('print_logi_version_' . $_POST['print_logi_id'], intval($_POST['logi_version']));
        if ($errmsg && !empty($errmsg)) {
            $errmsg .= "\n请将以上报错的打印单据作废，其它单据保存成功";
            exit($errmsg);
        }
        echo "SUCC";
    }

    /**
     * 保存发货单详情信息
     *
     */
    function doDetail()
    {
        $status = $_POST['status'] ? $_POST['status'] : 0;
        $ctl = $_POST['ctl'];
        $finder_id = $_GET['finder_id'];
        $this->begin('javascript:finderGroup["' . $finder_id . '"].refresh();');
        if (empty($_POST['dly'])) {
            $this->end(false, '保存失败');
        }
        if ($_POST['dly']['logi_id'] == '' || empty($_POST['dly']['logi_id'])) {
            $this->end(false, '请选择物流公司');
        }

        //获取wms发货通知单原始信息
        $deliveryObj = app::get('wms')->model('delivery');
        $delivery = $deliveryObj->dump($_POST['dly']['delivery_id']);
        $delivery['logi_no'] = kernel::single('wms_delivery_bill')->getPrimaryLogiNoById($_POST['dly']['delivery_id']);

        $arr_s = array('cancel', 'back', 'stop', 'return_back');
        if (in_array($delivery['status'], $arr_s) && $_POST['dly']['logi_no']) {
            $this->end(false, '发货单已撤销不能修改');
        }

        //取oms的发货通知单编号和ID
        $omeDlyObj = app::get('ome')->model('delivery');
        $omeDlyInfo = $omeDlyObj->dump(array('delivery_bn' => $delivery['outer_delivery_bn']), 'delivery_id,delivery_bn');

        //物流公司改变 物流单号不改变     将物流单号置空 重新计算物流费用
        $doObj = app::get('ome')->model('delivery_order');
        $oObj = app::get('ome')->model('orders');
        $Objdly_corp = app::get('ome')->model('dly_corp');
        $wmsCommonLib = kernel::single('wms_common');

        $corp = $Objdly_corp->dump($_POST['dly']['logi_id']);

        $order_ids = $doObj->getlist('order_id', array('delivery_id' => $omeDlyInfo['delivery_id']), 0, -1);

        if (count($order_ids) == 1) {
            $orders = $oObj->dump(array('order_id' => $order_ids[0]['order_id']), 'order_bn,shop_type');
        }

        if ($delivery['logi_id'] != $_POST['dly']['logi_id']) {

            if ($delivery['logi_no'] == $_POST['dly']['logi_no']) {
                $_POST['dly']['logi_no'] = NULL;
                $data['logi_no'] = NULL;
                if ($corp['type'] == 'DANGDANG') {
                    $data['logi_no'] = $orders['order_bn'];
                }
            } else {
                $data['logi_no'] = $_POST['dly']['logi_no'];
            }

            //如果切换了物流公司且用的是当当的判断是否合并发货单
            if ($corp['type'] == 'DANGDANG') {
                if (count($order_ids) > 1) {
                    $this->end(false, '此发货单是合并发货单,不可以选择当当物流!');
                }
                if ($orders['shop_type'] != 'dangdang') {
                    $this->end(false, '非当当店铺订单,不可以选择当当物流!');
                }

            }


            if ($corp['type'] == 'AMAZON' && $orders['shop_type'] != 'amazon') {
                $this->end(false, '此发货单是非亚马逊店铺订单,不可以选择亚马逊物流!');

            } //todo
            //计算预计物流费用
            $area = $_POST['dly_count'];
            $arrArea = explode(':', $delivery['consignee']['area']);

            $area_id = $arrArea[2];

            $price = $wmsCommonLib->getDeliveryFreight($area_id, $_POST['dly']['logi_id'], $delivery['net_weight']);
            $data['delivery_cost_expect'] = $price;

            //计算物流报价费用
            if ($delivery['logi_id']) {
                $dly_corp = $Objdly_corp->dump($_POST['dly']['logi_id']);
                $logi_name = $dly_corp['name'];
                //计算保价费用
                $protect = $dly_corp['protect'];
                if ($protect == 'true') {
                    $is_protect = 'true';
                    $protect_rate = $dly_corp['protect_rate']; //保价费率
                    $protect_price = $protect_rate * $delivery['net_weight'];
                    $minprice = $dly_corp['minprice']; //最低报价费用
                    if ($protect_price < $minprice) {
                        $cost_protect = $minprice;
                    } else {
                        $cost_protect = $protect_price;
                    }
                }
            }
            $data['cost_protect'] = $cost_protect ? $cost_protect : 0;
            $data['is_protect'] = $is_protect ? $is_protect : 'false';
        }

        if ($_POST['dly']['logi_no'] == '') {
            $_POST['dly']['logi_no'] = NULL;
        } else {
            if (kernel::single('wms_delivery_check')->existExpressNoBillByMain($_POST['dly']['logi_no'], $_POST['dly']['delivery_id'])) {
                $this->end(false, '已有此物流单号');
            }
        }
        $_POST['dly']['logi_no'] = $_POST['dly']['logi_no'] ? trim($_POST['dly']['logi_no']) : null;
        $dly['logi_id'] = $_POST['dly']['logi_id'];
        $dly['logi_no'] = $_POST['dly']['logi_no'];
        $dly['logi_name'] = $corp['name'];
        $dly['memo'] = $_POST['dly']['memo'];

        $result = $deliveryObj->update($dly, array('delivery_id' => $_POST['dly']['delivery_id']));

        //添加发货单修改物流单号的日志
        if ($_POST['dly']['logi_no'] && $_POST['dly']['delivery_id']) {
            $dlyLog['delivery_id'] = $_POST['dly']['delivery_id'];
            $dlyLog['logi_id'] = $_POST['dly']['logi_id'];
            $dlyLog['logi_no'] = $_POST['dly']['logi_no'];
            $dlyLog['logi_name'] = $corp['name'];
            $dlyLog['create_time'] = time();
            $dlyLogObj = app::get('wms')->model('delivery_log');
            if (!$dlyLogObj->dump(array('delivery_id' => $dlyLog['delivery_id'], 'logi_no' => $dlyLog['logi_no']))) {
                $dlyLogObj->save($dlyLog);
            }
        }

        //物流单号 或是 物流公司改变的时候 判断发货状态
        if ($delivery['logi_id'] != $_POST['dly']['logi_id'] || (!empty($delivery['logi_no']) && $delivery['logi_no'] != $_POST['dly']['logi_no'])) {
            //未发货的发货单变更物流单号应重新打印快递单
            if ($delivery['process'] == 'false') {
                $msg = '物流信息已改变，您应该从新打印快递单';
            }
        }

        $data['delivery_id'] = $_POST['dly']['delivery_id'];

        //根据真实重量计算实际物流运费
        if ($_POST['weight']) {
            $arrArea = explode(':', $delivery['consignee']['area']);

            $area_id = $arrArea[2];
            $data['delivery_cost_actual'] = $wmsCommonLib->getDeliveryFreight($area_id, $_POST['dly']['logi_id'], $_POST['weight']);//修改重量时更新物流费用
        }
        $data['weight'] = $_POST['weight'];//新增修改重量

        $deliveryObj->save($data);

        //仓储发货单主表信息变更，明细主记录也变更
        $deliveryBillObj = app::get('wms')->model('delivery_bill');
        $deliveryBillObj->update(array('logi_no' => $dly['logi_no'], 'delivery_cost_expect' => $data['delivery_cost_expect'], 'delivery_cost_actual' => $data['delivery_cost_actual'], 'weight' => $data['weight']), array('delivery_id' => $data['delivery_id'], 'type' => 1));

        if ($result) {
            if ($result === 1) {

                #[拆单]修改发货单详情加入发货单号_物流运单号 ExBOY
                $log_msg = '修改发货单详情';
                $log_msg .= (empty($delivery['delivery_bn']) ? '' : '，发货单号：' . $delivery['delivery_bn']);
                $log_msg .= (empty($delivery['delivery_bn']) ? '' : '，物流单号：' . $_POST['dly']['logi_no']);

                $opObj = app::get('ome')->model('operation_log');
                $opObj->write_log('delivery_modify@wms', $_POST['dly']['delivery_id'], $log_msg);
                //信息变更更新到oms
                $wms_id = kernel::single('ome_branch')->getWmsIdById($delivery['branch_id']);
                $data = array(
                    'delivery_bn' => $delivery['outer_delivery_bn'],
                    'weight' => $data['weight'],
                    'delivery_cost_actual' => $data['delivery_cost_actual'] ? $data['delivery_cost_actual'] : $delivery['delivery_cost_actual'],
                    'delivery_cost_expect' => $data['delivery_cost_expect'],
                    'logi_id' => $_POST['dly']['logi_id'],
                    'logi_no' => $_POST['dly']['logi_no'],
                    'logi_name' => $corp['name'],
                    'memo' => $_POST['dly']['memo'],
                    'action' => 'updateDetail',
                );

                $res = kernel::single('wms_event_trigger_delivery')->doUpdate($wms_id, $data, true);
            }
            $this->end(true, '保存成功' . $msg);
        }
        $this->end(false, '保存失败');
    }

    /**
     * 保存货品货位详情信息
     *
     */
    function doItemDetail()
    {
        $this->begin();
        if (empty($_POST['num']) || empty($_POST['pos'])) {
            $this->end(false, '请填写数量');
        }
        $dly_id = $_POST['delivery_id'];
        $number = $_POST['num'];
        $pos = $_POST['pos'];
        $Objdly = app::get('ome')->model('delivery');
        $delivery = $Objdly->dump($dly_id);
        $arr_s = array('succ', 'cancel', 'back', 'stop', 'return_back');
        if (in_array($delivery['status'], $arr_s) || $delivery['process'] == 'true') {
            $this->end(false, '发货单相关信息不能修改');
        }
        if ($number)
            foreach ($number as $key => $item) {
                $count = $item;
                foreach ($pos[$key] as $k => $i) {
                    $total += $i;
                }
                if ($total != $count) {
                    $this->end(false, '保存失败，填写总数不正确');
                }
                $count = 0;
                $total = 0;
            }

        $Objpos = app::get('ome')->model('dly_items_pos');
        if ($pos)
            foreach ($pos as $id => $row) {
                $Objpos->delete(array('item_id' => $id)); //更新前先做删除
            }
        //插入货品货位
        if ($pos)
            foreach ($pos as $key => $item) {
                foreach ($item as $k => $i) {
                    if ($i <= 0) {
                        continue;
                    }
                    $data['item_id'] = $key;
                    $data['num'] = $i;
                    $data['pos_id'] = $k;
                    $Objpos->save($data);
                    $data = '';
                }
            }
        $opObj = app::get('ome')->model('operation_log');
        $opObj->write_log('delivery_position@ome', $_POST['delivery_id'], '发货单货位录入');
        $this->end(true, '保存成功', 'index.php?app=wms&ctl=admin_receipts_print&act=index');
    }

    /**
     * 打印页面初始化 获取打印批次号
     *
     * @return Array
     */
    function _getPrintQueue($ids)
    {
        if (!$result = $this->_checkPrintQueue($ids, $msg)) {
            $this->message($msg);
            exit;
        }
        $queueObj = kernel::single('ome_queue');
        $queue = $queueObj->fetchPrintQueue($ids);

        return $queue;
    }

    /**
     * 检查是否能同批次打印
     *
     * @return bool
     */
    function _checkPrintQueue($ids, &$msg)
    {
        if (!empty($ids)) sort($ids);

        # 批量打印限制数量
        $deliCfgLib = kernel::single('wms_delivery_cfg');
        $sku = kernel::single('base_component_request')->get_get('sku');
        if ($sku == '') {
            $sku = kernel::single('base_component_request')->get_post('sku');
        }

        $batch_print_nums = $deliCfgLib->getValue('wms_batch_print_nums', $sku);
        if (count($ids) > $batch_print_nums) {
            $msg = "所选发货单号数量已超过批量打印数量 (" . $batch_print_nums . ")！";
            return false;
        }

        $delivery_check_ident = app::get('wms')->getConf('wms.delivery.check_ident');
        $delivery_check_ident = $delivery_check_ident ? $delivery_check_ident : 'on';
        $queueObj = kernel::single('ome_queue');
        if ($queueObj->isExistsQueueItems($ids, $existsQueueItems)) {
            if (count($ids) != count($existsQueueItems)) {
                $msg = "已生成批次号的发货单不能和未生成的发货单一起打印！";
            } else {
                $error = array();
                foreach ($existsQueueItems as $k => $v) {
                    if (!in_array($v, $error)) {
                        $error[] = $v;
                    }
                }
                $msg = "发货单号已存在有不相同的批次号：<br/>" . join('<br/>', $error);
            }
            if ($delivery_check_ident == 'on') {
                return false;
            } else {
                $this->pagedata['existsIdents'] = str_replace($msg, '<br/>', '&nbsp;&nbsp;');
            }
        }

        return true;
    }

    function message($msg)
    {

        $this->pagedata['err'] = 'true';
        $this->pagedata['base_dir'] = kernel::base_url();
        $this->pagedata['time'] = date("Y-m-d H:i:s");
        $this->pagedata['msg'] = $msg;
        $this->singlepage('admin/delivery/message.html');
        $this->display('admin/delivery/print.html');
    }

    function parsePrintIds($ids)
    {
        $result = array(
            'ids' => array(), //可用于打印的ID
            'errIds' => array(), //不能胜于打印的数据
            'errInfo' => array(), //所有错误信息
        );

        $dlyObj = &app::get('wms')->model('delivery');
        $dlyCheckLib = kernel::single('wms_delivery_check');

        foreach ($ids as $id) {
            $hasError = false;
            //检查当前订单的状态是不是可以打印
            if (!$dlyCheckLib->checkOrderStatus($id, true, $errMsg)) {
                //状态有问题的订单是肯定不要打印的
                $result['errIds'][] = $id;
                $result['errInfo'][$id] = $errMsg;
                $hasError = true;
            } else {
                $result['ids'][] = $id;
            }
            if (!$hasError) {
                //检查库存(除原样寄回发货单)
                $dly = $dlyObj->dump($id, '*', array('delivery_items' => array('*')));
                if ($dly['type'] == 'normal') {
                    foreach ($dly['delivery_items'] as $item) {
                        $re = $dlyObj->existStockIsPlus($item['product_id'], $item['number'], $item['item_id'], $dly['branch_id'], $err, $item['bn']);
                        if (!$re) {
                            $result['errIds'][] = $id;
                            $result['errInfo'][$id] .= $err . "&nbsp;,&nbsp;";
                            $hasError = true;
                        }
                    }
                }
                //库存有问题的单据认为是要打印的
                if (!in_array($id, $result['ids']))
                    $result['ids'][] = $id;
            }
        }

        if (empty($result['ids'])) {
            if (!empty($result['errIds'])) {
                $msg = sprintf("你所选择的 %d 张单据状态异常，无法打印，本次操作中止！", count($result['errIds']));
            } else {
                $msg = '你没有选择要打印的单据，请重新选择后再试！';
            }
            $this->message($msg);
            exit;
        }

        return $result;
    }

    /**
     * 设置订单样式
     * @param null
     * @return null
     */
    public function showPrintStyle()
    {
        $this->path[] = array('text' => '订单打印格式设置');
        $dbTmpl = app::get('ome')->model('print_tmpl_diy');
        $stockPrintTxt = $dbTmpl->get('wms', '/admin/delivery/stock_print');
        $deliveryPrintTxt = $dbTmpl->get('wms', '/admin/delivery/delivery_print');
        $mergePrintTxt = $dbTmpl->get('wms', '/admin/delivery/merge_print');
        $contentPurchase = $dbTmpl->get('purchase', '/admin/purchase/purchase_print');
        $contentPurchaseEo = $dbTmpl->get('purchase', '/admin/eo/eo_print');
        $contentPurchaseReturn = $dbTmpl->get('purchase', '/admin/returned/return_print');

        $this->pagedata['styleContent'] = $stockPrintTxt;
        $this->pagedata['styleContentDelivery'] = $deliveryPrintTxt;
        $this->pagedata['styleContentMerge'] = $mergePrintTxt;
        $this->pagedata['styleContentPurchase'] = $contentPurchase;
        $this->pagedata['styleContentPurchaseEo'] = $contentPurchaseEo;
        $this->pagedata['styleContentPurchaseReturn'] = $contentPurchaseReturn;
        $this->page('admin/delivery/printstyle.html');
    }

    /**
     * 保存订单打印样式
     * @param null
     * @return null
     */
    public function savePrintStyle()
    {
        $current_print = $_POST['current_print'];
        $dbTmpl = app::get('ome')->model('print_tmpl_diy');
        switch ($current_print) {
            case 'txtContent':
                $dbTmpl->set('wms', '/admin/delivery/stock_print', $_POST["txtContent"]);
                break;
            case 'txtContentDelivery':
                $dbTmpl->set('wms', '/admin/delivery/delivery_print', $_POST["txtContentDelivery"]);
                break;
            case 'txtContentMerge':
                $dbTmpl->set('wms', '/admin/delivery/merge_print', $_POST["txtContentMerge"]);
                break;
            case 'txtContentPurchase':
                $dbTmpl->set('purchase', '/admin/purchase/purchase_print', $_POST["txtContentPurchase"]);
                break;
            case 'txtContentPurchaseEo':
                $dbTmpl->set('purchase', '/admin/eo/eo_print', $_POST["txtContentPurchaseEo"]);
                break;
            case 'txtContentPurchaseReturn':
                $dbTmpl->set('purchase', '/admin/returned/return_print', $_POST["txtContentPurchaseReturn"]);
                break;
        }

        echo "<script>$$('.dialog').getLast().retrieve('instance').close();</script>";
    }

    /**
     * rebackPrintStyle
     *
     * @access public
     * @return void
     */
    public function rebackPrintStyle()
    {
        $current_print = $_POST['current_print'];
        $dbTmpl = app::get('ome')->model('print_tmpl_diy');
        switch ($current_print) {
            case 'txtContent':
                $dbTmpl->clear('wms', '/admin/delivery/stock_print');
                break;
            case 'txtContentDelivery':
                $dbTmpl->clear('wms', '/admin/delivery/delivery_print');
                break;
            case 'txtContentMerge':
                $dbTmpl->clear('wms', '/admin/delivery/merge_print');
                break;
            case 'txtContentPurchase':
                $dbTmpl->clear('purchase', '/admin/purchase/purchase_print');
                break;
            case 'txtContentPurchaseEo':
                $dbTmpl->clear('purchase', '/admin/eo/eo_print');
                break;
            case 'txtContentPurchaseReturn':
                $dbTmpl->clear('purchase', '/admin/returned/return_print');
                break;
        }

        echo "<script>$$('.dialog').getLast().retrieve('instance').close();</script>";
    }

    /**
     * 根据发货单ID修改它对映的发货配置
     * 一旦打印了任何一种单据就不
     *
     * @author chenping<chenping@shopex.cn>
     */
    private function updateDeliCfg($deliIds, $sku = '')
    {
        $filter = array(
            'delivery_id' => $deliIds,
            'stock_status' => 'false',
            'deliv_status' => 'false',
            'expre_status' => 'false',
        );
        $data = array(
            'deli_cfg' => $sku,
        );
        $deliModel = app::get('wms')->model('delivery');
        $deliModel->update($data, $filter);
    }

    public function orderbycreatetime($sku, $order_val, $op_id)
    {
        $base_url = 'index.php?app=wms&ctl=admin_receipts_print&act=index';
        $base_url .= ($sku == 'page_all' ? '&status=' : '&status=0&sku=' . $sku);

        $this->begin($base_url);

        if ($order_val == 1) {
            $value = 1;
        } else {
            $value = 0;
        }
        app::get('ome')->setConf('delivery.bycreatetime' . $op_id, $value);
        $this->end(true, '设置成功');
    }

    /**
     * @description 外部物流单号导入页
     * @access public
     * @param void
     * @author chenping<chenping@shopex.cn>
     * @return void
     */
    public function outerLogiIO()
    {
        $this->display('admin/delivery/outer_logi_io.html');
    }

    /**
     * @description 导出外部运单号模板
     * @access public
     * @param void
     * @author chenping<chenping@shopex.cn>
     * @return void
     */
    public function outerLogiTemplate()
    {
        $filename = "外部运单号模板" . date('Y-m-d') . ".csv";
        $encoded_filename = urlencode($filename);
        $encoded_filename = str_replace("+", "%20", $encoded_filename);
        $ua = $_SERVER["HTTP_USER_AGENT"];
        header("Content-Type: text/csv");
        if (preg_match("/MSIE/", $ua)) {
            header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
        } else if (preg_match("/Firefox$/", $ua)) {
            header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
        } else {
            header('Content-Disposition: attachment; filename="' . $filename . '"');
        }
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        $obj = app::get('ome')->model('delivery_outerlogi');
        $title = $obj->io_title();
        foreach ($title as $key => &$value) {
            $value = kernel::single('base_charset')->utf2local($value);
        }
        echo '"' . implode('","', $title) . '"';
    }

    public function shipStatus()
    {
        return array('succ' => '已发货', 'unsucc' => '未发货');#已发货状态以外的，都是未发货
    }

    /**
     * 打印新版快递单
     *
     * 修改 加了一个补打快递单的开关 wujian@shopex.cn 2012年3月14日
     */
    function toPrintExpre($afterPrint = true)
    {
        $_err = 'false';
        /* 单品、多品标识 */
        $sku = kernel::single('base_component_request')->get_get('sku');
        $sku = $sku ? $sku : '';

        $now_print_type = 'ship';

        //获取当前待打印的发货单过滤条件
        $filter_condition = $this->getPreparePrintIds();

        $PrintLib = kernel::single('wms_delivery_print');
         error_log('【'.json_encode($filter_condition)."】".PHP_EOL, 3, DATA_DIR . '/logs/toPrintExpre' . date('Y-m-d') . '.log');
        $print_data = $PrintLib->getPrintDatas($filter_condition, $now_print_type, $sku, $afterPrint, $msg);
        if (isset($msg['error_msg']) && $msg['error_msg']) {
            $this->headerErrorMsgDisply($msg['error_msg']);
        }

        if (isset($msg['warn_msg']) && $msg['warn_msg']) {
            $this->message($msg['warn_msg']);
            exit;
        }

        $waybillObj = kernel::single('logisticsmanager_service_waybill');
        $channel_extObj = app::get("logisticsmanager")->model("waybill_extend");
        $deliveryObj = app::get('wms')->model('delivery');
        $dlyBillObj = app::get('wms')->model('delivery_bill');
        $ids = $print_data['ids'];
        $channelObj = app::get("logisticsmanager")->model("channel");
        //防止并发打印重复获取运单号
        $_inner_key = sprintf("print_ids_%s", md5(implode(',', $ids)));
        $aData = cachecore::fetch($_inner_key);
        if ($aData === false) {
            cachecore::store($_inner_key, 'printed', 5);
        } else {
            $this->message("选中的发货单已在打印快递单中，请不要重复打印！！！如没有打印完成，请稍后重试！！！");
            exit;
        }
        //发起获取单号
        $expressDelivery = array();
        foreach ($print_data['deliverys'] as $val) 
        {
            empty($logiId) && $logiId = $val['logi_id'];
            foreach ($val as $dk => $dv) {
                if (!is_array($dv)) {
                    $expressDelivery[$val['delivery_id']][$dk] = $dv;
                }
            }
        }

        $corp = app::get('ome')->model('dly_corp')->dump($logiId);

        if (!isset($_GET['isdown']) && $corp['tmpl_type'] == 'electron') {//处理电子面单

            $eleRet = kernel::single('wms_delivery_electron')->dealElectron($expressDelivery, $corp['channel_id'], $afterPrint, $this);

            if (count($eleRet['id_bn'])) {
                foreach ($eleRet['id_bn'] as $k => $val) {
                    unset($print_data['deliverys'][$k]);
                    $print_data['errIds'][] = $k;
                    $print_data['errBns'][$k] = $val['bn'];
                    $print_data['errInfo'][$k] = $val['msg'];
                }
            }
        }
        foreach ($print_data['deliverys'] as $dk => $dv) {
            if ($expressDelivery[$dk]['logi_no']) $print_data['deliverys'][$dk]['logi_no'] = $expressDelivery[$dk]['logi_no'];

        }
        //判断是否京东补打
        if (!$afterPrint && count($_REQUEST['b_id']) > 0) {
            $childrenIds = $_REQUEST['b_id'];
            $main_dly_id = $ids[0];

            foreach ((array)$childrenIds as $k => $childrenId) {
                $children = $dlyBillObj->dump(array('b_id' => $childrenId, 'type' => 2), 'b_id,logi_no,status');
                $bill_id = $main_dly_id . "-" . $childrenId;
                $print_data['deliverys'][$bill_id] = $print_data['deliverys'][$main_dly_id];
                if ($children['logi_no']) $print_data['deliverys'][$bill_id]['logi_no'] = $children['logi_no'];

                unset($childrenId, $children);
                $delivery_data = $deliveryObj->dump($main_dly_id, 'logi_number');


            }
            $channel_info = $channelObj->dump(array('channel_id' => $corp['channel_id']), 'channel_type');
            if (in_array($channel_info['channel_type'], array('360buy')) && count($childrenIds) == $print_data['deliverys'][$main_dly_id]['logi_number'] - 1) {
            } else {
                //子单循环结束将原有主物流单信息删除
                unset($print_data['deliverys'][$main_dly_id]);
            }
        }

        $PrintShipLib = kernel::single('wms_delivery_print_ship');
        $format_data = $PrintShipLib->format($print_data, $sku, $_err);

        $this->pagedata = $format_data;
        $express_company_no = strtoupper($corp['type']);
        $objExpress = ome_print_tmpl_express::instance($express_company_no, $this);
        if (!$objExpress->getExpressTpl($corp)) {
            $msg = $objExpress->msg ? $objExpress->msg : '获取打印模板失败';
            $this->message($msg);
            exit();
        }
        $printField = $objExpress->printField;
        $printTpl = $objExpress->printTpl;
        $this->pagedata['printTmpl'] = $printTpl;

        $tradeIds = array();
        if ($format_data['delivery']) {
            foreach ($format_data['delivery'] as $val) {
                //获取快递单打印模板的servivce定义
                $data = array();
                foreach (kernel::servicelist('wms.service.template') as $object => $instance) {
                    if (method_exists($instance, 'getElementContent')) {
                        $tmp = $instance->getElementContent($val);
                    }
                    $data = array_merge($data, $tmp);
                }
                if (in_array($printTpl['template_type'], array('cainiao', 'cainiao_standard', 'cainiao_user'))) {
                    $printfieldtmp = array();
                    foreach ($printField as $pfield) {
                        $printfieldtmp[$pfield] = $data[$pfield];

                    }
                    $mydata[] = $printfieldtmp;
                } else {
                    $mydata[] = $data;
                }


                // 御城河订单
                $tradeIds = array_merge($tradeIds, explode(',', $val['order_bn']));
            }
        }


        $jsondata = $PrintShipLib->arrayToJson($mydata);

        //组织控件打印数据
        $this->pagedata['jsondata'] = $jsondata;
        $this->pagedata['data'] = addslashes($deliveryObj->array2xml2($mydata, 'data'));
        $this->pagedata['totalPage'] = count($mydata);


        /* 修改的地方 */
        if ($this->pagedata['printTmpl']['file_id']) {
            $this->pagedata['tmpl_bg'] = 'index.php?app=ome&ctl=admin_delivery_print&act=showPicture&p[0]=' . $this->pagedata['printTmpl']['file_id'];
        }

        //获取有问题的单据号
        $this->pagedata['errBns'] = $print_data['errBns'];
        $this->pagedata['err'] = $_err;

        //批次号
        $this->pagedata['idents'] = $print_data['identInfo']['items'];
        $this->pagedata['ident'] = join(',', $print_data['identInfo']['idents']);
        $this->pagedata['errIds'] = $print_data['errIds'];
        $this->pagedata['errInfo'] = $print_data['errInfo'];
        $items = array();
        foreach ($format_data['delivery'] as $row) {
            $items[$row['delivery_id']] = $row;
        }

        $this->pagedata['items'] = $items;
        $this->pagedata['sku'] = $sku;//单品 多品标识
        $this->pagedata['dpi'] = 96;
        $this->pagedata['base_dir'] = kernel::base_url();
        $this->pagedata['title'] = '快递单打印';
        $this->pagedata['uniqid'] = uniqid();

        if (!$afterPrint) {
            $this->pagedata['b_id'] = $_REQUEST['b_id'];

            $billFilter = array(
                'b_id' => $_REQUEST['b_id'],
            );
            $this->pagedata['bill_logi_no'] = $dlyBillObj->getList('b_id,logi_no', $billFilter);
            $this->pagedata['delibery_bill_flag'] = 'delibery_bill_flag';
        }

        //获取打印版本配置信息
        $logicfg = kernel::single('ome_print_logicfg')->getLogiCfg();
        if ($logicfg[$express_company_no]) {
            $logiVersionFlag = 1;
            $this->pagedata['logicfg'] = $logicfg[$express_company_no];
            $print_logi_version = app::get('ome')->getConf('print_logi_version_' . $this->pagedata['print_logi_id']);

            $this->pagedata['print_logi_version'] = intval($print_logi_version);
        }
        $this->pagedata['logiVersionFlag'] = $logiVersionFlag;
        $params = array('order_bn' => $this->pagedata['o_bn']);

        // 御城河
        /*
        $hchsafe = array(
            'operation' => '订单快递单打印',
            'tradeIds'  => $tradeIds,
        );
        kernel::single('base_hchsafe')->order_log($hchsafe);
        */
        // 御城河 END

        $objExpress->setParams($params)->getTmpl();
    }

    function covertNullToString(&$items)
    {
        foreach ($items as $k => &$v) {
            if ($v === null) {
                $v = "";
            } elseif (is_array($v)) {
                $this->covertNullToString($v);
            } else {
                $v = strval($v);
            }
        }
        return $items;
    }

    /**
     * 检查发货类型
     **/
    public function checkDeliveryType($id)
    {
        $dlyObj = app::get('wms')->model('delivery');
        $channelObj = app::get("logisticsmanager")->model("channel");
        $dlyCorpObj = app::get('ome')->model('dly_corp');
        $data = $dlyObj->dump($id, '*');
        $dlyCorp = $dlyCorpObj->dump($data['logi_id'], 'prt_tmpl_id,type,tmpl_type,channel_id,shop_id');
        $tpye = 'normal';
        //获取电子面单渠道
        $type = 'normal';
        $return = array('type' => 'normal');
        if ($dlyCorp['tmpl_type'] == 'electron') {
            $cFilter = array(
                'channel_id' => $dlyCorp['channel_id'],
                'status' => 'true',
            );
            $channel = $channelObj->dump($cFilter);
            $zlList = array('taobao', 'sf', 'yunda');
            if ($channel && in_array($channel['channel_type'], $zlList)) {
                $return = array('type' => 'zl', 'channel_type' => $channel['channel_type'], 'channel_id' => $channel['channel_id']);
            }
        }
        return $return;
    }

    /**
     * 检查物流单是否已经获取
     */
    public function checkWaybillOrderFinish($deliveryIds)
    {
        $dlyBillObj = app::get('wms')->model('delivery_bill');
        $deliveryIdStr = implode(',', $deliveryIds);
        $sql = "SELECT count(logi_no) as _count FROM `sdb_wms_delivery_bill` where delivery_id IN (" . $deliveryIdStr . ") and type=1";
        $result = $dlyBillObj->db->select($sql);
        $status = false;
        if ($result) {
            $count = $result[0]['_count'];
            if (count($deliveryIds) == $count) {
                $status = true;
            }
        }
        return $status;
    }

    /**
     * 获取电子面单运单号
     */
    public function getElectronLogiNo($directParam, $afterprint = true)
    {

        $urlParams = json_encode($directParam['get']);
        $postIds = json_encode($directParam['ids']);
        $request_uri = kernel::single('base_component_request')->get_request_uri() . '&isdown=1';
        $this->pagedata['urlParams'] = $urlParams;
        $this->pagedata['postIds'] = $postIds;
        $this->pagedata['channel'] = $directParam['channel'];
        $this->pagedata['directNum'] = $directParam['directNum'];
        $this->pagedata['request_uri'] = base64_encode($request_uri);

        if ($afterprint) {
            $this->singlepage('admin/delivery/controllertmpl/getelectronlogino.html');
            exit;
        } else {
            $cIds = json_encode($directParam['b_id']);
            $this->pagedata['cIds'] = $cIds;
            $this->singlepage('admin/delivery/controllertmpl/getelectronlogino_bill.html');
            exit;
        }
        exit();
    }

    /**
     *运单号异步页面
     */
    public function async_logino_page()
    {
        $channel_id = $_GET['channel_id'];
        $request_uri = base64_decode($_GET['request_uri']);
        $this->pagedata['channel_id'] = $channel_id;
        $this->pagedata['request_uri'] = $request_uri;
        $this->pagedata['MaxProcessOrderNum'] = intval($_GET['directNum']);
        $ids = explode(',', urldecode($_GET['itemIds']));

        //2为补打
        if ($_GET['after_print'] == 2) {
            $afterprint = $_GET['after_print'];
            $cIds = explode(',', urldecode($_GET['cIds']));
            $count = count($cIds);
            $this->pagedata['count'] = $count;
            $this->pagedata['postIds'] = json_encode($cIds);
            $this->pagedata['delivery_id'] = $ids[0];
            $this->display('admin/delivery/controllertmpl/async_bill_logino_page.html');
        } else {
            $this->pagedata['postIds'] = json_encode($ids);
            $count = count($ids);
            $this->pagedata['count'] = $count;
            $this->display('admin/delivery/controllertmpl/async_logino_page.html');
        }
    }

    public function getWaybillLogiNo()
    {

        $channel_id = $_POST['channel_id'];
        $delivery_id = $_POST['id'];

        $result = kernel::single('wms_event_trigger_logistics_electron')->directGetWaybill($delivery_id, $channel_id);
         error_log(var_export($delivery_id,true).'---'.var_export($channel_id,true).'【'.json_encode($result)."】".PHP_EOL, 3, DATA_DIR . '/logs/getWaybillLogiNo' . date('Y-m-d') . '.log');
        if ($result['dealResult']) {
            echo json_encode($result);
            exit();
        }

    }

    public function getExtLogiNo()
    {

        $channel_id = $_POST['channel_id'];
        $c_id = $_POST['cid'];
        $d_id = $_POST['did'];
        $result = kernel::single('wms_event_trigger_logistics_electron')->directGetWaybill($d_id, $channel_id, $c_id);

        if ($result['dealResult']) {
            echo json_encode($result);
            exit();
        }
    }


    #处理淘宝分销类型订单备注
    public function fomate_tbfx_memo($memo = null, $markShowMethod = 'last')
    {
        return '留言：' . preg_replace('/(买家|分销商|系统).*\(\d{4}-\d{1,2}-\d{1,2}\s{0,}\d{1,2}:\d{1,2}:\d{1,2}\)\s{0,}\(.*\)\s{0,}[:|：]/isU', '', $memo);
    }

    /*
     * 获取当前准备打印的发货单号
     */
    public function getPreparePrintIds()
    {
        $delivery_ids = $_REQUEST['delivery_id'];
          if(base_request::get_remote_addr()=='211.161.240.203')
        {
           error_log(var_export(array($delivery_ids),true),3,PUBLIC_DIR."/lakers12-22.log");
        }

	    $isSelectAll = $_REQUEST['isSelectedAll'];
        $printIds = array('filter' => '');

        $filter = $this->processFilter();

        //待打印,为避免重复打印，在后台，把相关打印字段加入到过滤条件中
        $filter_sql = null;
        if (($_GET['status'] === '0') && ($_GET['sku'] == '')) {
            $filter_sql = $this->getfiltersql();
        }

        if ($isSelectAll == '_ALL_') {
            if ($filter_sql) {
                $filter['filter_sql'] = $filter_sql;
            }

            $printIds['filter'] = $filter;
            return $printIds;

        } else {
            //去除值 为空，null，FALSE的key和value
            $delivery_ids = array_filter($delivery_ids);
            if ($delivery_ids) {
                if (is_array($delivery_ids)) {
                    $filter['delivery_id'] = $delivery_ids;
                    if ($filter_sql) {
                        $filter['filter_sql'] = $filter_sql;
                    }

                    $printIds['filter'] = $filter;
                    return $printIds;

                } else {
                    $printIds['filter'] = array('delivery_id' => $delivery_ids);
                    return $printIds;
                }
            } else {
                $this->headerErrorMsgDisply("请选择数据");
            }
        }
    }

    #为避免重复打印，在后台，把打印相关字段加入到过滤条件中
    function getfiltersql()
    {
        $deliCfgLib = kernel::single('wms_delivery_cfg');
        $btncombi = $deliCfgLib->btnCombi($_GET['sku']);
        #根据发货配置，只筛选未打印的
        switch ($btncombi) {
            case '1_1':
                $filter_sql = "((print_status & 1) !=1 or (print_status & 2) !=2 or (print_status & 4) !=4)";
                break;
            case '1_0':
                $filter_sql = "((print_status & 1) !=1 or (print_status & 4) !=4)";
                break;
            case '0_1':
                $filter_sql = "((print_status & 2) !=2 or (print_status & 4) !=4)";
                break;
            case '0_0':
                $filter_sql = "((print_status & 4) !=4)";
                break;
        }
        return $filter_sql;
    }

    /**
     * 显示信息
     */
    public function headerErrorMsgDisply($msg)
    {
        header("Content-type: text/html; charset=utf-8");
        exit($msg);
    }

    /**
     * 返回物流公司来源.
     * @param   delivery_id
     * @return  array
     * @access  public
     * @author sunjing@shopex.cn
     */
    public function getDeliveryType($id)
    {
        $deliveryObj = app::get('wms')->model('delivery');
        $channelObj = app::get("logisticsmanager")->model("channel");
        $dlyCorpObj = app::get('ome')->model('dly_corp');
        $data = $deliveryObj->dump($id, '*');
        $dlyCorp = $dlyCorpObj->dump($data['logi_id'], 'prt_tmpl_id,type,tmpl_type,channel_id,shop_id');
        $tpye = 'normal';
        //获取电子面单渠道
        $type = 'normal';
        $channel = array('type' => 'normal');

        if ($dlyCorp['tmpl_type'] == 'electron') {
            $cFilter = array(
                'channel_id' => $dlyCorp['channel_id'],
                'status' => 'true',
            );
            $channel = $channelObj->dump($cFilter);

        }
        return $channel;
    }


    /**
     * 发送至第三方
     * @
     * @
     * @access  public
     * @author sunjing@shopex.cn
     */
    function batch_sync()
    {
        $this->begin('');
        kernel::database()->exec('commit');
        $ids = $_POST['delivery_id'];
        $db = kernel::database();

        if (!empty($ids)) {
            foreach ($ids as $deliveryid) {
                $sql = "SELECT w.delivery_cost_actual,w.branch_id,w.delivery_id as wms_delivery_id,w.delivery_time,o.delivery_bn,o.delivery_id,w.outer_delivery_bn,w.weight FROM sdb_wms_delivery as w left join sdb_ome_delivery as o on w.outer_delivery_bn=o.delivery_bn WHERE w.status='3' AND o.process='false' AND w.delivery_id=" . $deliveryid;
                $deliverys = $db->select($sql);
                $deliveryObj = app::get('wms')->model('delivery');
                foreach ($deliverys as $delivery) {
                    $dly_id = $delivery['wms_delivery_id'];
                    $wms_id = kernel::single('ome_branch')->getWmsIdById($delivery['branch_id']);

                    $data = array(
                        'delivery_bn' => $delivery['outer_delivery_bn'],
                        'delivery_time' => $delivery['delivery_time'],
                        'weight' => $delivery['weight'],
                        'delivery_cost_actual' => $delivery['delivery_cost_actual'],
                    );
                    $res = kernel::single('wms_event_trigger_delivery')->consign($wms_id, $data, false);


                error_log(date('Y-m-d H:i:s') . '发送数据/' . var_export($data.'---'.$wms_id, true).'返回数据/' . var_export($res, true), 3, DATA_DIR . '/logs/print_batch_sync' . date('Y-m-d') . '.log');
                }


            }
        }
        $this->end(true, '命令已经被成功发送！！');
    }

    function ReCalc()
    {
        $this->page('admin/recalcexpressfee/recalc.html');
    }
    /**
     * 重算快递费
     * @
     * @
     * @access  public
     * @author sunjing@shopex.cn
     */
    function batchrecalc()
    {

        $this->begin('');
        $order_bns = $_POST['order_bns'];
        $db = kernel::database();

        $deliveryObj = app::get('wms')->model('delivery');
        $deliveryMdj = app::get('ome')->model('delivery');
        $omefeeLib    = kernel::single('ome_expressfee');
        $orderSplitLib    = kernel::single('ome_order_split');
        $order_bns=explode(',', $order_bns);
        if (!empty($order_bns)) 
        {
            foreach ($order_bns as $deliveryid) 
            {
                if(empty($deliveryid))
                {
                    continue;
                }
                $deliveryid=str_replace(array("\r\n", "\r", "\n"), "", $deliveryid);
                //根据ID查找关联表数据，调用方法重新核算重量
                    $sql="SELECT w.area_id, region.region_id,w.ship_area,w.logi_id,w.shop_id,w.net_weight,w.delivery_cost_actual,w.branch_id,w.delivery_id as wms_delivery_id,w.delivery_time,o.delivery_bn,
            o.delivery_id,w.outer_delivery_bn,w.weight,odo.order_id,a.deliverycomp_time,w.ship_city
            FROM sdb_wms_deliverycomp a 
            inner join sdb_wms_deliverycomp_line b on a.id = b.deliverycomp_id
            inner join sdb_wms_deliverynote_line d on b.deliverynote_id = d.deliverynote_id
            inner join sdb_wms_delivery w on d.delivery_bn = w.delivery_bn
            left join sdb_ome_delivery as o on w.outer_delivery_bn=o.delivery_bn 
            inner JOIN sdb_ome_delivery_order odo on odo.delivery_id =o .delivery_id 
            inner join sdb_wms_delivery_items items on items.delivery_id=w.delivery_id 
            inner join sdb_ome_shop s on w.shop_id=s.shop_id 
            left join sdb_eccommon_regions region on SUBSTRING_INDEX(substring_index(w.ship_area, ':', -2),':',-1)=region.region_id  WHERE o.logi_no='$deliveryid'  and w.delivery_id not in(SELECT delivery_id from sdb_wms_expressadjust_line)";
                $deliverys = $db->select($sql);
                if(!empty($deliverys))
                {
                    //校验是否结账期间内，否则不允许重算；

                    list($area_prefix,$area_chs,$area_id) = explode(':',$deliverys[0]['ship_area']);
                    list($privince,$city,$country) = explode('/',$area_chs);

                    $weight   = $orderSplitLib->getDeliveryWeight($deliverys[0]['order_id'],array(),$deliverys[0]['delivery_id']);

                    if(empty($deliverys[0]['region_id']))
                    {
                        //1. 先查区域，找到后直接返回
                        //2. 再查市区，找到后直接返回
                        //3. 最后查省，找到后直接返回
                        $calc_area_id=$this->getareaid($country,3);
                        if(empty($calc_area_id))
                        {
                            $calc_area_id=$this->getareaid($city,2);
                            if(empty($calc_area_id))
                            {
                                $calc_area_id=$this->getareaid($privince,1);
                                if(!empty($calc_area_id))
                                {
                                    $inparams['area_id']=$calc_area_id[0]['region_id'];   
                                }
                            }else
                            {
                                $inparams['area_id']=$calc_area_id[0]['region_id'];   
                            }
                        }else
                        {
                            $inparams['area_id']=$calc_area_id[0]['region_id'];   
                        }
                    }else{
                        $inparams['area_id']=$this->validateareaid($area_id,$city,$privince);
                    }
                    $inparams['logi_id']=$deliverys[0]['logi_id'];
                    $inparams['shop_id']=$deliverys[0]['shop_id'];
                    $inparams['weight']=$weight;
                    $inparams['delivery_time']=$deliverys[0]['deliverycomp_time'];
                    $inparams['ship_city']=$deliverys[0]['ship_city'];
                    $ship_area=$area_prefix.":".$area_chs.":".$inparams['area_id'];
                    $data['lmfee']=$omefeeLib->AutoCalcExpressfee($inparams);
                    $price = $deliveryMdj->getDeliveryFreightcity($inparams['ship_city'],$deliverys[0]['logi_id'],$weight);
                    if($price==0)
                    {
                        $failcount+=1;
                    }
                    $db->exec("update sdb_ome_delivery set lmfee=".$data['lmfee'].",area_id=".$inparams['area_id'].",ship_area='".$ship_area."',net_weight=$weight,weight=$weight,delivery_cost_actual=$price,delivery_cost_expect=$price,last_modified=".time()." where delivery_id=".$deliverys[0]['delivery_id']) ;
                    $db->exec("update sdb_wms_delivery set lmfee=".$data['lmfee'].",area_id=".$inparams['area_id'].",ship_area='".$ship_area."',net_weight=$weight,weight=$weight,delivery_cost_actual=$price,delivery_cost_expect=$price,last_modified=".time()." where delivery_id=".$deliverys[0]['wms_delivery_id']) ;
                    $db->exec("update sdb_wms_delivery_items set flatfeeprice=$price where delivery_id=".$deliverys[0]['wms_delivery_id']) ;   
                        
                }
            }
            $this->end(true, '已重算成功！！总数【'.count($order_bns).'】成功【'.(count($order_bns)-$failcount).'】失败【'.$failcount.'】');
        }else{
            $this->end(false, '无满足条件数据！！');
        }

    }
     //根据区域重新获取area
    function getareaid($area,$region_grade)
    {
        $sql="SELECT region_id from sdb_eccommon_regions where region_grade=$region_grade and local_name='$area'";
        return kernel::database()->select($sql);
    }

     //根据区域重新获取area
    function validateareaid($area,$city,$privince)
    {
        $sql="SELECT p_region_id from sdb_eccommon_regions where region_id=".$area;
        $data= kernel::database()->select($sql);
        if(!empty($data[0]['p_region_id']))
        {
            return $area; 

        }else{

            $sql1="SELECT region_id from sdb_eccommon_regions where region_grade=2 and local_name='$city'";
            $data1= kernel::database()->select($sql1);
            if(!empty($data1))
            {
                return $data1[0]['region_id'];
            }else{

                $sql2="SELECT region_id from sdb_eccommon_regions where region_grade=1 and local_name='$privince'";
                $data2= kernel::database()->select($sql2);
                if(!empty($data2))
                {
                    return $data2[0]['region_id'];
                }
            }
        }
    }
    function batch_oms()
    {
        $this->begin('');
        $db = kernel::database();
        $sql = "SELECT w.delivery_cost_actual,w.branch_id,w.delivery_id as wms_delivery_id,w.delivery_time,o.delivery_bn,o.delivery_id,w.outer_delivery_bn,w.weight FROM sdb_wms_delivery as w left join sdb_ome_delivery as o on w.outer_delivery_bn=o.delivery_bn WHERE w.status='3' AND o.process='false' limit 0,1000 ";

        $deliverys = $db->select($sql);
        $deliveryObj = app::get('wms')->model('delivery');
        foreach ($deliverys as $delivery) {
            $dly_id = $delivery['wms_delivery_id'];
            $wms_id = kernel::single('ome_branch')->getWmsIdById($delivery['branch_id']);

            $data = array(
                'delivery_bn' => $delivery['outer_delivery_bn'],
                'delivery_time' => $delivery['delivery_time'],
                'weight' => $delivery['weight'],
                'delivery_cost_actual' => $delivery['delivery_cost_actual'],
            );
            $res = kernel::single('wms_event_trigger_delivery')->consign($wms_id, $data, true);
        }
        $this->end(true, '等待配货成功');

    }

    function sendAxCustom()
    {

        $specId = $_POST['specId'];
        $time = date("Y-m-d", time());
        $this->pagedata['time'] = $time;
        $this->page('admin/print/sendAxCustom.html');

    }

    //快递费
    public function doSendAxCustom()
    {

        $this->begin('');
        $limitTime = $_POST['time'];
        $srcorg = trim($_POST['srcorg']);
        $delivery_time = strtotime($limitTime);
        $db = kernel::database();
        $sql = "select count(1) as total from sdb_wms_deliverykdf where srcorg= '".$srcorg."' and  delivery_time=" . $delivery_time;
        $kdfdata = $db->select($sql);
        if ($kdfdata[0]['total'] > 0) {
            $this->end(false, '该天该组织数据已经发送成功过！');
        }
        $sql = "select
            d.delivery_bn,d.delivery_cost_actual,d.net_weight,di.number,mbm.weight
            from sdb_wms_delivery d
            LEFT JOIN  sdb_wms_delivery_items di
            on d.delivery_id=di.delivery_id
            LEFT JOIN sdb_material_basic_material_ext  mbm
            on mbm.bm_id= di.product_id
            where d.delivery_time > " . $delivery_time . " AND d.delivery_time<" . $delivery_time . "+86399
            and  (d.delivery_cost_actual ='0' or
            d.net_weight ='0' or di.number ='0' or mbm.weight ='0' or  di.flatFeePrice<=0  )";
        $errordata = $db->select($sql);
        //验证费用是否合格
        if (count($errordata) > 0) {
            $bns = '';
            foreach ($errordata as $key => $val) {
                $bns .= $val['delivery_bn'] . ",";
            }
            $msg = '发货单：' . $bns . '/快递费存在异常!';
            print_r($msg);
            exit();
        }

        $sql="SELECT
            group_concat(d.delivery_bn) delivery_bns,
            group_concat(ooder.order_id) order_ids,
            s.shop_type,d.ship_province,d.logi_name,s.shop_bn,s.name,s.customername,
            s.AXCustomer,dc.AxTyoe,wdi.bn,SUM(wdi.number) as number,wdi.product_id,wdi.product_name,
            SUM(wdi.flatFeePrice) as flatFeePrice,
            wdi.price,wdi.sale_price,wdl.axshipno,d.net_weight,d.delivery_bn,d.delivery_cost_actual,
            mbm.weight,mbm.unit FROM  sdb_wms_deliverycomp dcp
            LEFT JOIN sdb_wms_deliverycomp_line dcl ON dcp.id = dcl.deliverycomp_id
            LEFT JOIN  sdb_wms_deliverynote_line wdl on wdl.deliverynote_id = dcl.deliverynote_id
            LEFT JOIN sdb_wms_delivery d ON d.delivery_id = wdl.delivery_id
            LEFT JOIN sdb_ome_dly_corp dc ON dc.corp_id = d.logi_id
            LEFT JOIN sdb_wms_deliverynote dn ON dn.id = wdl.deliverynote_id
            LEFT JOIN sdb_wms_delivery_items wdi ON wdi.delivery_id = d.delivery_id
            LEFT JOIN sdb_material_basic_material_ext mbm on mbm.bm_id= wdi.product_id
            LEFT JOIN sdb_ome_delivery odel on odel.delivery_bn=d.outer_delivery_bn
            LEFT JOIN sdb_ome_delivery_order odo on odo.delivery_id =odel.delivery_id
            LEFT JOIN sdb_ome_orders ooder on ooder.order_id=odo.order_id
            LEFT JOIN sdb_ome_shop s ON d.shop_id = s.shop_id
            WHERE  dcp.deliverycomp_time > ".$delivery_time." AND  dcp.deliverycomp_time<".$delivery_time."+86400  AND s.AXCustomer !='' and dcp.srcorg='".$srcorg."'  GROUP BY s.customername,d.ship_province,d.logi_name,wdi.bn,wdl.axshipno, s.shop_bn";
        $rows = $db->select($sql);
        foreach ($rows as $key => $row) {

            $sql="select
                  SUM(ooderi.nums) as itemNum,
                  SUM(ooderi.divide_order_fee) as divide_order_fee,
                  SUM(ooderi.sale_price) as item_sale_price
                  from  sdb_ome_order_items ooderi where
                  ooderi.item_type='product' AND ooderi.delete='false' AND ooderi.order_id in
                  (".$row['order_ids'].") and ooderi.bn='".$row['bn']."'";
            $itemGroup=$db->selectRow($sql);
            if(!isset($itemGroup['itemNum'])){
                $itemGroup['itemNum']=0;
            }
            if(!isset($itemGroup['divide_order_fee'])){
                $itemGroup['divide_order_fee']=0;
            }
            if(!isset($itemGroup['item_sale_price'])){
                $itemGroup['item_sale_price']=0;
            }

            $price=$row['flatFeePrice'];
            $param['list'][$key]['parmCostPrice'] = $price;
            $param['list'][$key]['parmCustAccount'] = $row['AXCustomer'];//客户编号
            $param['list'][$key]['parmDeliveryArea'] = $row['ship_province'];//快递区域
            $param['list'][$key]['parmDeliveryWeight'] = $row['weight'] * $row['number'];//快递重量
            $param['list'][$key]['parmExpressCompany'] = $row['AxTyoe'];//快递公司
            $param['list'][$key]['parmItemId'] = $row['bn'];//物料编号
            $param['list'][$key]['parmItemName'] = $row['product_name'];//物料名称
            $param['list'][$key]['parmPackingSlipId'] = $row['axshipno'];//装箱单号
            $param['list'][$key]['parmServicePrice'] = $price;//服务费（新添加字段）
            $param['list'][$key]['parmShopID'] = $row['shop_bn'];//店铺编号
            $param['list'][$key]['parmShopName'] = $row['name'];//店铺名称
            $param['list'][$key]['parmDeliveryQty'] = $row['number'];//料品数量
            $param['list'][$key]['parmRetailPrice'] =  round($itemGroup['item_sale_price']/$itemGroup['itemNum'],4);//原价
            $param['list'][$key]['parmDealerPrice'] =  round($itemGroup['divide_order_fee']/$itemGroup['itemNum'],4); //销售价格
            $param['list'][$key]['parmDeliverUnit'] = $row['unit'];//单位
            $param['list'][$key]['parmDeliverDate'] = date('Y/m/d', $delivery_time);//送货日期
            // $param['list'][$key]['parmSyncLog'] = $row['delivery_bns'];//delivery_bns
            $salesPlatfrom = '其他';
            if ($row['shop_type'] != '') {
                $salesPlatfrom = ome_shop_type::shop_name($row['shop_type']);
            }
            $param['list'][$key]['parmPlatformName'] = $salesPlatfrom;//平台
        }
        if (count($param['list']) > 0) {
            try {
                date_default_timezone_set("PRC");
                ini_set('soap.wsdl_cache_enabled', "0"); //关闭wsdl缓存
                ini_set('default_socket_timeout', 500);    // timeout
                $param['company']=$srcorg;
                if(WITH_QIMEN){
                    $res=kernel::single('erpapi_rpc_qimen')->call('InsSalesFreight',$param);
                    $res=json_decode($res,1);
                }else{
                    $webservice_url = HOST_AXToOMs;
                    $client = new SoapClient($webservice_url);
                    $res = $client->__soapCall('InsSalesFreight', array('parameters' => $param));
                    $res['response']['data'] = $res->InsSalesFreightResult;
                }
                error_log(date('Y-m-d H:i:s') . '发送数据/' . var_export($param, true) . '返回数据/' . var_export($res, true), 3, DATA_DIR . '/logs/kdf' . date('Y-m-d') . '.log');
                $res_msg = $res['response']['data'];
                if (isset($res_msg) && $res_msg == '0') {
                    //存日志
                    $data['srcorg']=$srcorg;
                    $data['delivery_time'] = $delivery_time;
                    $data['create_time'] = time();
                    $data['send_log'] = json_encode($param['list']);
                    app::get('wms')->model('deliverykdf')->save($data);
                    $this->end(true, '发送成功');
                } else {
                    print_r($res_msg);
                }
            } catch (Exception $e) {
                echo $e->getMessage();
            }
        } else {
            $msg = '这一天该组织没有数据!';
            $this->end(false, $msg);
        }

    }
}
